w
、a
、w+
、a+
和r+
模式之间的确切区别是什么?特别地,文档暗示所有这些模式都允许写入文件,并且指出它们分别打开了文件以进行“追加”、“写入”和“更新”,但没有定义这些术语的含义。
w
、a
、w+
、a+
和r+
模式之间的确切区别是什么?打开模式与C标准库函数fopen()
的一样。
BSD fopen
页面将其定义为:
The argument mode points to a string beginning with one of the following
sequences (Additional characters may follow these sequences.):
``r'' Open text file for reading. The stream is positioned at the
beginning of the file.
``r+'' Open for reading and writing. The stream is positioned at the
beginning of the file.
``w'' Truncate file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.
``w+'' 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.
``a'' Open for writing. The file is created if it does not exist. The
stream is positioned at the end of the file. Subsequent writes
to the file will always end up at the then current end of file,
irrespective of any intervening fseek(3) or similar.
``a+'' Open for reading and writing. The file is created if it does not
exist. The stream is positioned at the end of the file. Subse-
quent writes to the file will always end up at the then current
end of file, irrespective of any intervening fseek(3) or similar.
w
和w+
都可以做到“如果文件不存在,则创建文件”的功能。 - Wei Yangb
到打开文件的模式中会使文件以二进制模式打开,因此还有rb
、wb
和r+b
等模式。Python在Windows上区分文本文件和二进制文件;当读取或写入数据时,文本文件中的行尾字符会自动稍作修改。 - user4458796a
,w
还是r
模式下都没有一个一致的独立作用,这样说对吗?或者我是没看到规律?那么这个规律是什么? - Charlie Parkerr+
不仅打开文件以供读取,还可以进行写入,而w+
则反过来,不仅打开文件以供写入,还可以进行读取。对于a+
同样如此。 - balu我注意到有时候需要再次谷歌 fopen,只是为了建立不同模式之间主要区别的心理图像。因此,我认为下次阅读起来更快的方法是使用一个图表。也许其他人会发现这很有帮助。
+
基本上表示写入。奇怪的是 w
并不意味着这个,而是意味着截断。(阅读下一个答案后,似乎 w
表示覆盖写入,而 a
则表示追加写入,这更有意义…)如果文件不存在,你对创建文件有什么评论吗? - Charlie Parkera
的描述是错误的。写入总是定位在末尾。 - Antti Haapala -- Слава УкраїніSubsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek(3) or similar
,这比仅仅说初始位置在末尾要强一些。 - jcai相同的信息,只是以表格形式呈现
| r r+ w w+ a a+
------------------|--------------------------
read | + + + +
write | + + + + +
write after seek | + + +
create | + + + +
truncate | + +
position at start | + + + +
position at end | + +
定义如下:
read - 允许从文件中读取内容
write - 允许向文件中写入内容
create - 若文件不存在,则创建该文件
truncate - 打开文件时清空文件(即擦除文件中的所有内容)
position at start - 文件打开后,初始位置设置为文件开头
position at end - 文件打开后,初始位置设置为文件结尾
注意:a
和 a+
总是将内容追加到文件末尾,忽略任何 seek
操作。
另外,在我的 win7 / python2.7 上以 a+
模式打开新文件时有趣的行为如下:
write('aa'); seek(0, 0); read(1); write('b')
—— 第二个 write
被忽略
write('aa'); seek(0, 0); read(2); write('b')
—— 第二个 write
抛出 IOError
open(file,'a');close();open(file,'r +')
来完成此操作。 - pinhead这里的选项与C标准库中的fopen函数相同:
w
截断文件,覆盖已存在的内容。
a
追加到文件,添加到已存在的内容之后。
w+
读写打开文件,截断文件并允许您读取已写入文件的内容。
a+
追加和读取打开文件,允许您追加到文件并读取其内容。
r | r+ | x | x+ | w | w+ | a | a+ | |
---|---|---|---|---|---|---|---|---|
可读 | √ | √ | √ | √ | √ | |||
可写 | √ | √ | √ | √ | √ | √ | √ | |
默认位置:开头 | √ | √ | √ | √ | √ | √ | ||
默认位置:结尾 | √ | √ | ||||||
必须存在 | √ | √ | ||||||
不能存在 | √ | √ | ||||||
加载时截断(清空文件) | √ | √ | ||||||
始终写入 EOF | √ | √ |
模式
t(默认) | b | |
---|---|---|
str (io.TextIOBase ) |
√ | |
bytes (io.BufferedIOBase ) |
√ |
如果未选择模式,则使用文本模式(t
)。 因此,r
与rt
相同。
seek(0)
(将光标移动到文件开头),当你使用 write("foo")
时,文件也会写入到文件末尾。这就是为什么它被称为 "append" 模式。你总是将内容追加到文件的末尾。 - Peilonrayz我在试图弄清楚为什么要使用'mode'w+'而不是'w'的情况下,偶然发现了这个。最终,我进行了一些测试。我认为'mode'w+'没有太多用处,因为在两种情况下,文件都会被截断。但是,在使用'w+'的情况下,您可以通过寻找回读写内容。如果您尝试使用'w'进行任何读取,它将引发IOError。使用'mode'w+'时,如果不使用seek进行读取,将无法获得任何结果,因为文件指针将位于您写入的位置之后。
我认为需要注意的是,Python 3对于打开模式的定义不同于这里正确的Python 2。
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists
----
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newlines mode (for backwards compatibility; should not be used in new code)
模式r
、w
、x
、a
与模式修饰符b
或t
结合使用。可选择添加+
,应避免使用U
。
正如我通过艰难的方式发现的那样,总是在文本模式下打开文件时指定t
是一个好主意,因为在标准的open()
函数中,r
是rt
的别名,但在所有压缩模块的open()
函数中(例如读取*.bz2
文件时),r
是rb
的别名。
因此,打开文件的模式应为:
rt
/wt
/xt
/at
用于以文本模式读取/写入/创建/附加到文件和
rb
/wb
/xb
/ab
用于以二进制模式读取/写入/创建/附加到文件。
像以前一样使用+
。