当前位置:   article > 正文

Python文件操作之open()的mode_fap mode

fap mode

open()的mode

mode can be:

  • 'r' when the file will only be read, 只读,
  • 'w' for only writing (an existing file with the same name will be erased), 只写,
  • and 'a' opens the file for appending; any data written to the file is automatically added to the end (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position). 追加。

  • If mode is omitted, it defaults to 'r'.

  • The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. 在text mode,'\n'会被自动转换成与系统相关的换行符。考虑到移植性,建议使用'b' binary mode。

  • The file will be created if it doesn’t exist when opened for writing or appending; it will be truncated when opened for writing. 用w或a模式打开文件时,若文件不存在则自动被创建;用w模式打开文件时,若文件已存在则内容被截断清空。

  • Add a '+' to the mode to allow simultaneous reading and writing. Modes 'r+', 'w+' and 'a+' open the file for updating (reading and writing); note that 'w+' truncates the file.

表格:

mode文件不存在文件存在流位置
rIOError只读√IOErrorbeginning
w创建截空truncateIOError只写√beginning
a创建保留unchangedIOError只写√beginning/end
r+IOErrorbeginning
w+创建截空truncatebeginning
a+创建保留unchangedbeginning/end
  • 注意&拓展:
    在C语言中:

    As you can see, ‘+’ requests a stream that can do both input and output. When using such a stream, you must call fflush (see Stream Buffering) or a file positioning function such as fseek (see File Positioning) when switching from reading to writing or vice versa. Otherwise, internal buffers might not be emptied properly.

    —— 出处 libc

r

Open text file for reading. The stream is positioned at the beginning of the file.

模式'r'的流程:

Created with Raphaël 2.1.0 开始 文件存在? 原内容保留、流位置0L 何种操作(读or写)? close() 结束 写=IOError[X] IOError[X] yes no yes no
  • 文件不存在:IOError错误
>>> fr = open(r'E:\python_workspace\file_r.txt', 'r')

Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    fr = open(r'E:\python_workspace\file_r.txt', 'r')
IOError: [Errno 2] No such file or directory: 'E:\\python_workspace\\file_r.txt'
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 文件存在:流位置,读。参考官方文档_读写文件
    To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string. size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned.

    If the end of the file has been reached, f.read() will return an empty string ( "").
>>> fr = open(r'E:\python_workspace\file_r.txt', 'r')
>>> fr.tell()
0L
>>> fr.read()
'hello\nworld\n'
>>> fr.tell()
14L
>>> fr.read()
''
>>> fr.tell()
14L
>>> fr.close()
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 文件存在:写=IOError错误
>>> fr = open(r'E:\python_workspace\file_r.txt', 'r')
>>> fr.tell()
0L
>>> fr.write('NiceToMeetYou')

Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
    fr.write('NiceToMeetYou')
IOError: File not open for writing
>>> fr.close()
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

w

Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.

模式'w'的流程:

Created with Raphaël 2.1.0 开始 文件存在? 内容清空truncate 流位置0L 何种操作(读or写)? 写、flush() close() 结束 读=IOError[X] 自动创建文件 yes no yes no
  • 文件不存在:自动创建之
>>> fw=open(r'E:\python_workspace\file_w.txt', 'w')  # 文件被自动创建
>>> fw.closed
False
>>> fw.close()
>>> fw.closed
True
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 文件存在:文件打开时已有内容被清空;读=IOError
>>> fw=open(r'E:\python_workspace\file_w.txt', 'w')  # 文件已有内容被清空
>>> fw.closed
False
>>> fw.read()

Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    fw.read()
IOError: File not open for reading
>>> fw.tell()
0L
>>> fw.write('good bye')
>>> fw.read()

Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    fw.read()
IOError: File not open for reading
>>> fw.close()
>>> fw.closed
True
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 文件存在:写文件
>>> fw=open(r'E:\python_workspace\file_w.txt', 'w')
>>> fw.tell()  # 此时内容为空
0L
>>> fw.write('hello,world')
>>> fw.flush()  # 此时内容'hello,world'
>>> fw.tell()
11L
>>> fw.seek(2,0)
>>> fw.tell()
2L
>>> fw.write('zzz')
>>> fw.flush()  # 此时内容'hezzz,world'
>>> fw.tell()
5L
>>> fw.close()
>>> fw.closed
True
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

a

Open for appending (writing at end of file). The file is created if it does not exist.
The stream is positioned at the end of the file(可能存在系统差异).

模式'a'的流程:

Created with Raphaël 2.1.0 开始 文件存在? 文件内容保留 流位置0L/SEEK_END(可能存在系统差异) 何种操作(读or写)? 写、flush() close() 结束 读=IOError[X] 自动创建文件 yes no yes no
  • 文件不存在:自动创建之
>>> fa=open(r'E:\python_workspace\file_a.txt', 'a')  # 文件被自动创建
>>> fa.closed
False
>>> fa.tell()
0L
>>> fa.close()
>>> fa.closed
True
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 文件存在:流位置,写=追加;读=IOError
>>> fa=open(r'E:\python_workspace\file_a.txt', 'a')  # 文件已有内容'HelloWorld'
>>> fa.tell()
0L
>>> fa.read()

Traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
    fa.read()
IOError: File not open for reading
>>> fa.tell()
0L
>>> fa.write('GoodBye')
>>> fa.tell()
17L
>>> fa.flush()  # 此时文件内容'HelloWorldGoodBye'
>>> fa.close()
>>> fa.closed
True
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • ‘a’模式下,write操作追加到end,而忽略当前的seek位置(可能存在系统差异):
>>> fa=open(r'E:\python_workspace\file_a.txt', 'a')  # 文件已有内容'HelloWorld'
>>> fa.closed
False
>>> fa.tell()
0L
>>> fa.write('NiceToMeetYou')
>>> fa.flush()  # 文件现在内容'HelloWorldNiceToMeetYou'
>>> fa.tell()
23L
>>> fa.seek(3, 0)
>>> fa.tell()
3L
>>> fa.write('GoodBye')
>>>> fa.flush()  # 文件现在内容'HelloWorldNiceToMeetYouGoodBye'
>>> fa.tell()
30L
>>> fa.close()
>>> fa.closed
True
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

r+, plus

Open for reading and writing. The stream is positioned at the beginning of the file.

  • 文件不存在:IOError错误
>>> frp=open(r'E:\python_workspace\file_rp.txt', 'r+')

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    frp=open(r'E:\python_workspace\file_rp.txt', 'r+')
IOError: [Errno 2] No such file or directory: 'E:\\python_workspace\\file_rp.txt'
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 文件存在,原文件为空文件:流位置,读,写:
>>> frp=open(r'E:\python_workspace\file_rp.txt', 'r+')  # 原文件内容为空
>>> frp.closed
False
>>> frp.tell()
0L
>>> frp.read()
''
>>> frp.write('HelloWorld')
>>> frp.flush()  # 文件内容为'HelloWorld'
>>> frp.tell()
10L
>>> frp.read()
''
>>> frp.seek(0, 0)
>>> frp.tell()
0L
>>> frp.read()
'HelloWorld'
>>> frp.tell()
10L
>>> frp.seek(3, 0)
>>> frp.tell()
3L
>>> frp.read()
'loWorld'
>>> frp.seek(3, 0)
>>> frp.tell()
3L
>>> frp.write('zzz')
>>> frp.flush()  # 文件内容为'Helzzzorld'
>>> frp.tell()
6L
>>> frp.seek(0, 0)
>>> frp.tell()
0L
>>> frp.read()
'Helzzzorld'
>>> frp.close()
>>> frp.closed
True
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 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
  • 文件存在,原文件为非空文件:流位置,读,写:
>>> frp=open(r'E:\python_workspace\file_rp.txt', 'r+')  # 原文件内容非空
>>> frp.closed
False
>>> frp.tell()
0L
>>> frp.read()
'HelloWorld'
>>> frp.tell()
10L
>>> frp.seek(3, 0)
>>> frp.tell()
3L
>>> frp.write('zzz')
>>> frp.flush()  # 文件内容为'Helzzzorld'
>>> frp.tell()
6L
>>> frp.seek(0, 0)
>>> frp.tell()
0L
>>> frp.read()
'Helzzzorld'
>>> frp.close()
>>> frp.closed
True
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

w+, plus

Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.

  • 文件不存在:自动创建之
>>> fwp=open(r'E:\python_workspace\file_wp.txt', 'w+')  # 文件被自动创建
>>> fwp.closed
False
>>> fwp.name
'E:\\python_workspace\\file_wp.txt'
>>> fwp.mode
'w+'
>>> fwp.close()
>>> fwp.closed
True
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 文件存在:流位置,读,写:
>>> fwp=open(r'E:\python_workspace\file_wp.txt', 'w+')  # 文件已有内容被清空
>>> fwp.closed
False
>>> fwp.tell()
0L
>>> fwp.read()
''
>>> fwp.tell()
0L
>>> fwp.write('HelloWorld\nNiceToMeetYou\nBye')  # 注意`\n`会被自动转换成与系统相关的换行符
>>> fwp.flush()  # 文件内容'HelloWorld\nNiceToMeetYou\nBye'三行
>>> fwp.tell()
30L
>>> fwp.seek(0, 0)
>>> fwp.tell()
0L
>>> fwp.read()
'HelloWorld\nNiceToMeetYou\nBye'
>>> fwp.close()
>>> fwp.closed
True
>>> import os
>>> os.name
'nt'
>>> os.linesep
'\r\n'
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

a+, plus

Open for reading and appending (writing at end of file). The file is created if it does not exist.
The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

  • 文件不存在:自动创建之
>>> fap=open(r'E:\python_workspace\file_ap.txt', 'a+')  # 文件自动被创建
>>> fap.closed
False
>>> fap.tell()
0L
>>> fap.name
'E:\\python_workspace\\file_ap.txt'
>>> fap.mode
'a+'
>>> fap.close()
>>> fap.closed
True
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 文件存在:流位置,读,写=追加到end而忽略当前的seek位置
>>> fap=open(r'E:\python_workspace\file_ap.txt', 'a+')  # 原文件内容非空,为'HelloWorld'
>>> fap.closed
False
>>> fap.tell()
0L
>>> fap.read()
'HelloWorld'
>>> fap.tell()
10L
>>> fap.seek(0, 0)
>>> fap.tell()
0L
>>> fap.write('Bye')
>>> fap.flush()  # 追加,文件内容'HelloWorldBye'
>>> fap.tell()
13L
>>> fap.seek(0, 0)
>>> fap.tell()
0L
>>> fap.read()
'HelloWorldBye'
>>> fap.close()
>>> fap.closed
True
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • ‘+’模式的流位置:’+’模式下可同时读和写,是否使用同一个流位置?例子:
>>> fap=open(r'E:\python_workspace\file_ap.txt', 'a+')  # 文件已有内容'HelloWorld'
>>> fap.closed
False
>>> fap.tell()
0L
>>> fap.read()
'HelloWorld'
>>> fap.tell()
10L
>>> fap.seek(fap.tell(), 0)
>>> fap.write('GoodBye')
>>> fap.flush()  # 文件内容'HelloWorldGoodBye'
>>> fap.tell()
17L
>>> fap.seek(fap.tell(), 0)
>>> fap.read()
''
>>> fap.tell()
17L
>>> fap.seek(0, 0)
>>> fap.tell()
0L
>>> fap.seek(fap.tell(), 0)
>>> fap.tell()
0L
>>> fap.write('SeeYou')
>>> fap.flush()  # 文件内容'HelloWorldGoodByeSeeYou'
>>> fap.tell()
23L
>>> fap.seek(fap.tell(), 0)
>>> fap.tell()
23L
>>> fap.read()  # '+'模式下读和写使用同一个流位置。同时'a'模式又有其独有特点即读是流位置、写是追加到end并更新流位置
''
>>> fap.tell()
23L
>>> fap.close()
>>> fap.closed
True
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 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

结论:’+’模式下读和写使用同一个流位置。同时’a’模式又有其独有特点即读是当前流位置、写是追加到end(即尾位置)并更新流位置

b 二进制模式

the 'b' is ignored on all POSIX conforming systems, including Linux. (Other systems may treat text files and binary files differently, and adding the ‘b’ may be a good idea if you do I/O to a binary file and expect that your program may be ported to non-UNIX environments.)

例子,对比text mode与binary mode的不同,特别是换行'\n'造成的区别:

>>> import os
>>> os.name
'nt'
>>> os.linesep
'\r\n'
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
>>> fwp=open(r'E:\python_workspace\file_wp.txt', 'w+')  # 不存在的文件被自动创建
>>> fwp.tell()
0L
>>> fwp.write('HelloWorld\nNiceToMeetYou\n')  # ====注意text mode下'\n'换行被自动转换成与平台相关
>>> fwp.flush()
>>> fwp.tell()
27L
>>> fwp.seek(0, 0)
>>> fwp.tell()
0L
>>> fwp.read()
'HelloWorld\nNiceToMeetYou\n'
>>> fwp.tell()
27L
>>> fwp.close()
>>> fwp.closed
True
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
>>> fwpb=open(r'E:\python_workspace\file_wpb.txt', 'wb+')  # 不存在的文件被自动创建
>>> fwpb.tell()
0L
>>> fwpb.name
'E:\\python_workspace\\file_wpb.txt'
>>> fwpb.mode
'wb+'
>>> fwpb.write('HelloWorld\nNiceToMeetYou\n')
>>> fwpb.flush()  # 文件内容'HelloWorld\nNiceToMeetYou\n',====注意binary mode下'\n'换行不发生转换,是Linux的LF换行方式
>>> fwpb.tell()
25L
>>> fwpb.seek(0, 0)
>>> fwpb.tell()
0L
>>> fwpb.read()
'HelloWorld\nNiceToMeetYou\n'
>>> fwpb.tell()
25L
>>> fwpb.seek(0, 2)  # 若不执行此seek则write会发生IOError错误吗?
>>> fwpb.tell()
25L
>>> fwpb.write('Welcome\r\nTo\r\nChina\r\n')
>>> fwpb.flush()  # 追加文件内容'Welcome\r\nTo\r\nChina\r\n',换行方式是Windows的CRLF换行
>>> fwpb.tell()
45L
>>> fwpb.seek(0, 0)
>>> fwpb.tell()
0L
>>> fwpb.read()
'HelloWorld\nNiceToMeetYou\nWelcome\r\nTo\r\nChina\r\n'
>>> fwpb.tell()
45L
>>> fwpb.close()
>>> fwpb.closed
True
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/278391?site
推荐阅读
相关标签
  

闽ICP备14008679号