赞
踩
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模式打开文件时,若文件已存在则内容被截断清空。
'+'
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 | 文件不存在 | 文件存在 | 读 | 写 | 流位置 |
---|---|---|---|---|---|
r | IOError | √ | 只读√ | IOError | beginning |
w | 创建 | 截空truncate | IOError | 只写√ | beginning |
a | 创建 | 保留unchanged | IOError | 只写√ | beginning/end |
r+ | IOError | √ | √ | √ | beginning |
w+ | 创建 | 截空truncate | √ | √ | beginning |
a+ | 创建 | 保留unchanged | √ | √ | beginning/end |
在C语言中:
As you can see,‘+’
requests a stream that can do both input and output. When using such a stream, you must callfflush
(see Stream Buffering) or a file positioning function such asfseek
(see File Positioning) when switching from reading to writing or vice versa. Otherwise, internal buffers might not be emptied properly.
—— 出处 libc
Open text file for reading. The stream is positioned at the beginning of the file.
模式'r'
的流程:
>>> 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'
>>>
To read a file’s contents, callf.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()
>>>
>>> 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()
>>>
Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
模式'w'
的流程:
>>> fw=open(r'E:\python_workspace\file_w.txt', 'w') # 文件被自动创建
>>> fw.closed
False
>>> fw.close()
>>> fw.closed
True
>>>
>>> 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
>>>
>>> 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
>>>
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'
的流程:
>>> fa=open(r'E:\python_workspace\file_a.txt', 'a') # 文件被自动创建
>>> fa.closed
False
>>> fa.tell()
0L
>>> fa.close()
>>> fa.closed
True
>>>
>>> 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
>>>
>>> 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
>>>
Open for reading and writing. The stream is positioned at the beginning of the file.
>>> 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'
>>>
>>> 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
>>>
>>> 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
>>>
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
>>>
>>> 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'
>>>
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
>>>
>>> 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
>>>
>>> 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
>>>
结论:’+’模式下读和写使用同一个流位置。同时’a’模式又有其独有特点即读是当前流位置、写是追加到end(即尾位置)并更新流位置
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'
>>>
>>> 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
>>>
>>> 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
>>>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。