reading/writing files in Python
file types:
- plaintext files, such as .txt .py
- Binary files, such as .docx, .pdf, iamges, spreadsheets, and executable programs(.exe)
steps to read/write files
- call the
open()
function to return aFile object
- Call the
read()
orwrite()
method on the File object - Close the file by calling the
close()
method on the File object
To open the file in 'reading plaintext' mode (read mode):
>>> helloFile=open('/user/kaiming/Python/hello.txt')
>>> helloFile=open('/user/kaiming/Python/hello.txt', 'r')
where 'r' stands for read mode
the call to open()
returns a File object
, and assigned to the variable helloFile
To get a list of string values from the file, one string for each line of text, use readline()
function
Writing to files >>> open ('hello.txt', 'w') # write mode >>> open ('hello.txt', 'a') # append mode
Note:
- when a file is opened in read mode, Python lets you only read data from
the file; you can't write or modify it in any way.
- if the filename passed to
open()
does not exist, both
write and append mode will create a new, blank file
>>> baconFile = open('bacon.txt', 'w') # create a blank file named 'bacon.txt'
>>> baconFile.write('Hello world!\n')
13
>>> baconFile.close()
>>> baconFile = open('bacon.txt', 'a')
>>> baconFile.write('Bacon is not a vegetable.')
25
>>> baconFile.close()
>>> baconFile = open('bacon.txt')
>>> content = baconFile.read()
>>> baconFile.close()
>>> print(content)
Hello world!
Bacon is not a vegetable.
reading/writing files in Python的更多相关文章
- Huge CSV and XML Files in Python, Error: field larger than field limit (131072)
Huge CSV and XML Files in Python January 22, 2009. Filed under python twitter facebook pinterest lin ...
- Working with Excel Files in Python
Working with Excel Files in Python from: http://www.python-excel.org/ This site contains pointers to ...
- Reading Csv Files with Text_io in Oracle D2k Forms
Below is the example to read and import comma delimited csv file in oracle forms with D2k_Delimited_ ...
- Unsupervised Image-to-Image Translation Networks --- Reading Writing
Unsupervised Image-to-Image Translation Networks --- Reading Writing 2017.03.03 Motivations: most ex ...
- PostgreSQL Reading Ad Writing Files、Execution System Instructions Vul
catalog . postgresql简介 . 文件读取/写入 . 命令执行 . 影响范围 . 恶意代码分析 . 缓解方案 1. postgresql简介 PostgreSQL 是一个自由的对象-关 ...
- Creating Excel files with Python and XlsxWriter(通过 Python和XlsxWriter来创建Excel文件(xlsx格式))
以下所有内容翻译至: https://xlsxwriter.readthedocs.io/ #----------------------------------------------------- ...
- 【Selenium】【BugList4】执行pip报错:Fatal error in launcher: Unable to create process using '""D:\Program Files\Python36\python.exe"" "D:\Program Files\Python36\Scripts\pip.exe" '
环境信息: python版本:V3.6.4 安装路径:D:\Program Files\python36 环境变量PATH:D:\Program Files\Python36;D:\Program F ...
- Read Large Files in Python
I have a large file ( ~4G) to process in Python. I wonder whether it is OK to "read" such ...
- How to read and write multiple files in Python?
Goal: I want to write a program for this: In a folder I have =n= number of files; first read one fil ...
随机推荐
- 51Nod 1522 上下序列 —— 区间DP
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1522 区间DP,从大往小加: 新加入一种数有3种加法:全加左边,全 ...
- Eclipse中直接执行sql语句(图文说明)
转自:https://blog.csdn.net/changjyzzu/article/details/45487847 1.首先新建sql文件,然后打开文件 22.右键点击空白处,点击set-con ...
- java笔记线程方式1等待终止
public final void join():等待该线程终止 public class ThreadJoinDemo { public static void main(String[] args ...
- LVS集群体系和调度算法
集群体系和调度算法 LVS集群体系架构 1)使用LVS架设的服务器集群系统有三个部分组成: 最前端的负载均衡层,用Load Balancer表示, 中间的服务器群组层,用Server Array表示, ...
- mysql通用分页存储过程遇到的问题(转载)
mysql通用分页存储过程遇到的问题(转载) http://www.cnblogs.com/daoxuebao/archive/2015/02/09/4281980.html
- thinkphp 5 常用的助手函数
load_trait:快速导入Traits,PHP5.5以上无需调用 /** * 快速导入Traits PHP5.5以上无需调用 * @param string $class t ...
- Java 8 (10) CompletableFuture:组合式异步编程
随着多核处理器的出现,提升应用程序的处理速度最有效的方式就是可以编写出发挥多核能力的软件,我们已经可以通过切分大型的任务,让每个子任务并行运行,使用线程的方式,分支/合并框架(java 7) 和并行流 ...
- 专题九:实现类似QQ的即时通信程序
引言: 前面专题中介绍了UDP.TCP和P2P编程,并且通过一些小的示例来让大家更好的理解它们的工作原理以及怎样.Net类库去实现它们的.为了让大家更好的理解我们平常中常见的软件QQ的工作原理,所以在 ...
- phpstorm设置代码片段
tab 向后推进 shift+tab 向前推进 ctrl+d 复制整行 ctrl+Y删除整行 代码片段就是代码快捷键,如果你设置了www.baidu.com这些内容,但是不想一直重复的打,可以设置个代 ...
- Elasticsearch--建议器
目录 可用的建议器类型 term建议器 term建议器的配置选项 phrase建议器 completion建议器 在考虑性能的情况下,允许用户的拼写错误,以及构建一个自动完成功能 可用的建议器类型 t ...