Python file read and write summary

Is the most common file read and write IO operations. Python built-in functions to read and write files, usage, and C are compatible.

Before reading and writing files, we first have to look at, read and write files on the disk functions are provided by the operating system, modern operating system does not allow normal operation of the program directly to disk, read and write files is requesting the operating system to open a file object (commonly referred to as a file descriptor), then the interface provided by the operating system reads data (file read) from this file object, or to write data to the file object (write file).

Remember a python file operation function of three methods: (everything is an object of the python)

Open () : requests to open files:

  If the file exists return operation object files,

  If the file does not exist, it will throw an exception

read method - to read a file,the file is read into memory, all the contents of the file can be read one time

write method - writing files to file

close method - to close the file

Prepare your document

 

Past the wind - Chin 
words: Xu Changde 
song: Tu Hui Yuan 
your shadow everywhere 
people's mind like a dust 
falls past drifting into the future 
fall out of tears to the eyes of 
the experience of life with great emotion 
and sometimes lonely than embrace it 
so that spring to mind let Mengqiu to 
let you go 
reluctant to forget 
everything for love 
no regrets And I 
let the wind are all past the wind the wind heart as you move 
yesterday, the flower faded flowers is not a dream is not a dream is not a dream

First, read the file

1, open the file

To open a file with mode read a file object, use the built-in Python open()function, passing the file name and identifier.

First, we look at the definition of open function:

1 def open(file: Union[str, bytes, int], mode: str = ..., buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., newline: Optional[str] = ..., closefd: bool = ...) Inferred type: (file: Union[str, bytes, int], mode: str, buffering: int, encoding: Optional[str], errors: Optional[str], newline: Optional[str], closefd: bool) -> IO Open file and return a stream. Raise IOError upon failure. 

In general, we just need to open the file path to the incoming file, the contents of the file we read here is Chinese, in order to avoid distortion, we are here to specify the encoding format. When we use other parameters in these instructions carefully. The open function returns an object that represents a file, python object will be stored in our variable f in, so that we can easily operate.

I create a text file named test.txt on the desktop:

>>> f =open('C:\Users\24414\Desktop\test.txt','r',encoding='utf-8')//以绝对路径打开文件
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

This error occurs because: windows path is a backslash \, however backslash \ escape character has a meaning in python, so write windows file path py file, pay particular attention to the backslash \usage of.

The following three solutions:

1 >>> f =open('C:\\Users\\24414\\Desktop\\test.txt','r',encoding='utf-8')
2 >>>
3 >>> f =open(r'C:\Users\24414\Desktop\test.txt','r',encoding='utf-8')
4 >>>
5 >>> f =open('C:/Users/24414/Desktop/test.txt','r',encoding='utf-8')
6 >>>

Identifier 'r' for read, so that we can successfully open a file.

If the file does not exist, open()the function will throw an IOErrorerror and gives detailed information about the error code and tell you the file does not exist:

1 >>> f =open('C:\Users\24414\Desktop\test1.txt','r',encoding='utf-8')
2   File "<stdin>", line 1
3 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
4 >>>

2, read the file

2.1 reads the entire file

The first step is to document the operation was to open the file to be operated, and then read it. In python we can use the open function to open a file, and then use the read method to read the file.

Example 1, data.txt and our files in the same directory, so you can directly use the name ::

If other path (non-program directory), then the direct use of absolute path:
I was in the window of the development environment, with windows, for example, we have a data1.txt file on the desktop: After we open the file, print it this f some of what in the end:

F = Open >>> (R & lt ' C: \ the Users \ 24414 \ Desktop \ DATA1.TXT ' , ' R & lt ' , encoding = ' GBK ' )
 >>> reached, f.read ()
 ' past the wind - Chin \ n words: Xu Changde \ n song: Tu Hui yuan \ n your shadow everywhere \ n person's mind like a dust \ n drifting past fall next \ n fall out of tears to the eyes \ n experience of life with great emotion \ n sometimes lonely than embrace it \ n spring to let the heart to let Mengqiu \ n let you go \ n reluctant to forget \ n everything for love \ n no regrets and my \ n let the wind are all past the wind with the wind heart as you move \ n yesterday flower faded flowers is not a dream is not a dream is not a dream ' 
>>>

 

print (dir (f))

Output:

['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', '_finalizing', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline', 'readlines', 'reconfigure', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'write_through', 'writelines']
>>>

这些都是和文件操作相关的,在这里不会给大家做仔细说明,只是让大家对这些东西有个印象,等遇到了,我们在说怎么使用。

2.2 逐行读取

上面是读取整个文件内容,接下来我们看下怎么逐行读取文件。
在逐行读取文本的时候,常见的可以使用for循环来读取:

2.2.1 read()

read:如果指定了参数 size,就按照该指定长度从文件中读取内容,否则,就读取全文。被读出来的内容,全部塞到一个字符串里面。这样有好处,就是东西都到内存里面了,随时取用;但如果文件内容太多了,内存会吃不消。注意换行符也为占用一个内存,缺点需要知道光标的位置,以及每一行的大小SIZE。

>>> f = open(r'C:\Users\24414\Desktop\data1.txt', 'r',encoding='gbk')
>>> f.read(10)
'往事随风 - 齐秦\n'
>>> f.read(5)
'词:许常德'
>>>

2.2.2 readline()

概述:readline() 方法用于从文件读取整行,包括 "\n" 字符。从字面意思可以看出,该方法每次读出一行内容。所以读取时占用内存小,比较适合大文件,该方法返回一个字符串对象。如果指定了一个非负数的参数,则返回指定大小的字节数,包括 "\n" 字符。

 语法

fileObject.readline(); 

参数

  • size -- 从文件中读取的字节数。

返回值

返回从字符串中读取的字节。

 1 >>> f = open(r'C:\Users\24414\Desktop\data1.txt', 'r',encoding='gbk')
 2 >>> while True:
 3 ...     line=f.read()
 4 ...     if not line:#到 EOF,返回空字符串,则终止循环
 5 ...             break
 6 ...     print(line)
 7 ...
 8 往事随风 - 齐秦
 9 词:许常德
10 曲:涂惠元
11 你的影子无所不在
12 人的心事像一颗尘埃
13 落在过去 飘向未来
14 掉进眼里就流出泪来
15 曾经沧海无限感慨
16 有时孤独比拥抱实在
17 让心春去 让梦秋来
18 让你离开
19 舍不得忘
20 一切都是为爱
21 没有遗憾 还有我
22 就让往事随风 都随风 都随风 心随你动
23 昨天花谢花开 不是梦 不是梦 不是梦
24 >>>

 

 2.2.3 readlines()

概述:readlines() 方法用于读取所有行(直到结束符 EOF)并返回列表,该列表可以由 Python 的 for... in ... 结构进行处理。当文件太大时,内存可能不够用。 如果碰到结束符 EOF 则返回空字符串。

如果碰到结束符 EOF 则返回空字符串。

语法

readlines() 方法语法如下:

fileObject.readlines( );

参数

  • 无。

返回值

返回列表,包含所有的行。

 1 >>> f = open(r'C:\Users\24414\Desktop\data1.txt', 'r',encoding='gbk')
 2 >>> print(f.readlines())
 3 ['往事随风 - 齐秦\n', '词:许常德\n', '曲:涂惠元\n', '你的影子无所不在\n', '人的心事像一颗尘埃\n', '落在过去 飘向未来\n', '掉进眼里就流出泪来\n', '曾经沧海无限感慨\n', '有时孤独比拥抱实在\n', '让心春去 让梦秋来\n', '让你离开\n', '舍不得忘\n', '一切都是为爱\n', '没有遗憾 还有我\n', '就让往事随风 都随风 都随风 心随你动\n', '昨天花谢花开 不是梦 不是梦 不是梦']
 4 >>> for line in f.readlines():
 5 ...     print(line)
 6 ...
 7 >>>
 8 >>>
 9 >>> f = open(r'C:\Users\24414\Desktop\data1.txt', 'r',encoding='gbk')
10 >>> for line in f.readlines():
11 ...     print(line)
12 ...
13 往事随风 - 齐秦
14 
15 词:许常德
16 
17 曲:涂惠元
18 
19 你的影子无所不在
20 
21 人的心事像一颗尘埃
22 
23 落在过去 飘向未来
24 
25 掉进眼里就流出泪来
26 
27 曾经沧海无限感慨
28 
29 有时孤独比拥抱实在
30 
31 让心春去 让梦秋来
32 
33 让你离开
34 
35 舍不得忘
36 
37 一切都是为爱
38 
39 没有遗憾 还有我
40 
41 就让往事随风 都随风 都随风 心随你动
42 
43 昨天花谢花开 不是梦 不是梦 不是梦
44 >>>

 

 2.3 读取大文件

如果文件太大,就不能用 read() 或者 readlines() 一次性将全部内容读入内存。

方法一、可以使用 while 循环和 readlin() 来完成这个任务。

这里不再举例

方法二、使用 fileinput 模块:

 1 >>> import fileinput
 2 >>> for line in fileinput.input(r'C:\Users\24414\Desktop\data1.txt'):
 3 ...     print(line)
 4 ...
 5 往事随风 - 齐秦
 6 
 7 词:许常德
 8 
 9 曲:涂惠元
10 
11 你的影子无所不在
12 
13 人的心事像一颗尘埃
14 ......

 

第三种方法,直接读取文件句柄,推荐使用这种方法

 1 >>> f = open(r'C:\Users\24414\Desktop\data1.txt', 'r',encoding='gbk')
 2 >>> f
 3 <_io.TextIOWrapper name='C:\\Users\\24414\\Desktop\\data1.txt' mode='r' encoding='gbk'>
 4 >>> for line in f:
 5 ...     print(line)
 6 ...
 7 往事随风 - 齐秦
 8 
 9 词:许常德
10 
11 曲:涂惠元
12 
13 你的影子无所不在
14 
15 人的心事像一颗尘埃

之所以能够如此,是因为 file 是可迭代的数据类型,直接用 for 来迭代即可

3、 关闭文件

 最后一步是调用close()方法关闭文件。文件使用完毕后必须关闭,因为文件对象会占用操作系统的资源,并且操作系统同一时间能打开的文件数量也是有限的:

1 f.close()

 

 每次都要使用完都要关闭文件,很容易忘记,而且每次都这么写实在太繁琐,所以,Python引入了with语句来自动帮我们调用close()方法:

 1 >>> with open(r'C:\Users\24414\Desktop\data1.txt', 'r',encoding='gbk') as f:
 2 ...     for line in f:
 3 ...             print(line.strip())
 4 ...
 5 往事随风 - 齐秦
 6 词:许常德
 7 曲:涂惠元
 8 你的影子无所不在
 9 人的心事像一颗尘埃
10 落在过去 飘向未来
11 掉进眼里就流出泪来
12 曾经沧海无限感慨

 注意:

 

file-like Object

 

open()函数返回的这种有个read()方法的对象,在Python中统称为file-like Object。除了file外,还可以是内存的字节流,网络流,自定义流等等。file-like Object不要求从特定类继承,只要写个read()方法就行。

 

StringIO就是在内存中创建的file-like Object,常用作临时缓冲。

 

二进制文件

 

前面讲的默认都是读取文本文件,并且是UTF-8编码的文本文件。要读取二进制文件,比如图片、视频等等,用'rb'模式打开文件即可:

 

字符编码

 

要读取非UTF-8编码的文本文件,需要给open()函数传入encoding参数,例如,读取GBK编码的文件:

 

1 >>> f = open('/Users/michael/gbk.txt', 'r', encoding='gbk')
2 >>> f.read()
3 '测试'

 

遇到有些编码不规范的文件,你可能会遇到UnicodeDecodeError,因为在文本文件中可能夹杂了一些非法编码的字符。遇到这种情况,open()函数还接收一个errors参数,表示如果遇到编码错误后如何处理。最简单的方式是直接忽略:

 

1 >>> f = open('/Users/michael/gbk.txt', 'r', encoding='gbk', errors='ignore')

二、写文件

写文件和读文件是一样的,唯一区别是调用open()函数时,传入标识符'w'或者'wb'表示写文本文件或写二进制文件

>>> f = open(r'C:\Users\24414\Desktop\data2.txt', 'w',encoding='gbk')
>>> f.write('Hello world!')
12
>>>f.flush()

注意:如果指定路径没有data2.txt文件,它会先创建一个文件。

当我们写文件时,操作系统往往不会立刻把数据写入磁盘,而是放到内存缓存起来,空闲的时候再慢慢写入。如果没有这句代码f.flush(),内容只是被写到了缓冲区,等缓冲区满了,才会将内容写到文件中。有了这句代码就会直接写入到文本中,而不会等待缓冲区满。当然也可以最后,调用f.close()实现相同的功能。没有这一步的后果是数据可能只写了一部分到磁盘,剩下的丢失了。

python文件对象提供了两个“写”方法: write() 和 writelines()。

  • write()方法和read()、readline()方法对应,是将字符串写入到文件中。
1 >>> with open(r'C:\Users\24414\Desktop\data2.txt','w',encoding='gbk') as f:
2 ...     f.write('1\n2\n3\n')
3 ...
4 6
5 >>>

 

  • writelines()方法和readlines()方法对应,也是针对列表的操作。它接收一个字符串列表作为参数,将他们写入到文件中,换行符不会自动的加入,因此,需要显式的加入换行符。

 

f1 = open(r'C:\Users\24414\Desktop\data2.txt'
, 'w') 
f1.writelines([
"1", "2", "3"])

# 此时test1.txt的内容为:123
f1
= open(
r'C:\Users\24414\Desktop\data2.txt', 'w')
f1.writelines([
"1\n", "2\n", "3\n"])
# 此时test1.txt的内容为: # 1 # 2 # 3

 

Guess you like

Origin www.cnblogs.com/weststar/p/11346857.html