在 python 内置的 open 函数中,模式 w
、a
、w+
、a+
和 r+
之间的确切区别是什么?
特别是,文档暗示所有这些都将允许写入文件,并说它打开文件是为了“附加”、“写入”和“更新”,但没有定义这些术语的含义。
打开方式与 C 标准库函数fopen()
完全相同。
The BSD fopen
manpage 将它们定义如下:
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.
我注意到时不时地我需要重新搜索一遍谷歌 fopen,只是为了建立一个关于模式之间主要区别的心理形象。所以,我认为下次阅读图表会更快。也许其他人也会觉得这很有帮助。
https://d20p74l5mne5au.cloudfront.net/ExWNT-white-bg.png
+
基本上意味着写作。这很奇怪,w
并不意味着它,但它意味着截断......(在阅读下一个答案之后,似乎 w
写入并且 a
代表附加。这更有意义......)如果文件不存在,您对文件创建有什么意见吗?
a
描述错误。写入始终位于末尾。
Subsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek(3) or similar
,这比仅仅说 initial 位置是结束要强一些。
相同的信息,只是表格形式
| 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
移动。
顺便说一句。对于以 a+
模式打开的新文件,至少在我的 win7 / python2.7 上有趣的行为:
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+')
来完成此操作。
a
和 a+
写入将始终发生在文件末尾,无论是否使用 seek()
手动移动指针。
选项与 C 标准库中的 fopen function 相同:
w
截断文件,覆盖已经存在的文件
a
附加到文件,添加到已经存在的任何内容
w+
打开读取和写入,截断文件,但也允许您读回写入文件的内容
a+
打开以进行附加和读取,允许您附加到文件并读取其内容
r+ x x+ w w+ a a+ 可读 xxxxx 可写 xxxxxxx 默认位置:开始 xxxxxx 默认位置:结束 xx 必须存在 xx 不能存在 xx 加载时截断(清除文件) xx 始终写入 EOF xx
模式
(默认) b str (io.TextIOBase) x 字节 (io.BufferedIOBase) x
如果没有选择模式;使用文本模式 (t
)。因此 r
与 rt
相同。
seek(0)
(将光标移动到文件的开头)然后当您 write("foo")
文件将写入文件末尾。因此,为什么它被称为“附加”模式。您总是附加到文件的末尾。
我认为这对于跨平台执行(即作为 CYA)来说很重要。 :)
在 Windows 上,附加到模式的 'b' 以二进制模式打开文件,因此还有 'rb'、'wb' 和 'r+b' 等模式。 Windows 上的 Python 区分了文本文件和二进制文件;读取或写入数据时,文本文件中的行尾字符会自动稍作更改。这种对文件数据的幕后修改适用于 ASCII 文本文件,但它会破坏 JPEG 或 EXE 文件中的二进制数据。在读写此类文件时要非常小心使用二进制模式。在 Unix 上,将“b”附加到模式并没有什么坏处,因此您可以独立于平台使用它来处理所有二进制文件。
这直接引用自 Python Software Foundation 2.7.x。
我偶然发现了这个,试图弄清楚为什么你会使用模式“w+”而不是“w”。最后,我只是做了一些测试。我看不出模式“w+”有多大用途,因为在这两种情况下,文件都会被截断。但是,使用“w+”,您可以在写完后通过回溯来阅读。如果您尝试使用“w”进行任何读取,则会引发 IOError。不使用模式 'w+' 的搜索读取不会产生任何结果,因为文件指针将在您写入的位置之后。
我发现重要的是要注意 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
是个好主意,因为 r
是标准 open()
函数中 rt
的别名,但 {5 } 在所有压缩模块的 open()
函数中(例如读取 *.bz2
文件时)。
因此打开文件的模式应该是:
rt
/ wt
/ xt
/ at
用于以文本模式读取/写入/创建/附加到文件和
rb
/ wb
/ xb
/ ab
用于以二进制模式读取/写入/创建/附加到文件。
像以前一样使用 +
。
w
和w+
都可以做The file is created if it does not exist
b
以二进制模式打开文件,因此还有rb
、wb
和r+b
等模式。 Windows 上的 Python 区分了文本文件和二进制文件;读取或写入数据时,文本文件中的行尾字符会自动稍作更改。a
、w
或r
,+
不会做一致独立的事情?还是我没有看到模式?模式是什么?