ChatGPT解决这个技术问题 Extra ChatGPT

内置 open 函数中模式 a、a+、w、w+ 和 r+ 的区别?

在 python 内置的 open 函数中,模式 waw+a+r+ 之间的确切区别是什么?

特别是,文档暗示所有这些都将允许写入文件,并说它打开文件是为了“附加”、“写入”和“更新”,但没有定义这些术语的含义。

您提供的链接准确定义了这些值。您提供的链接的哪些部分您看不到或无法理解?您能否澄清您的问题以解释您对该链接的不理解之处?
@ChrisB。 - 我在 bugs.python.org/issue19627 将此报告为错误
是否没有简单而单一的文档来解释 + 符号的含义?

A
Arnav Borborah

打开方式与 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.

我相信您的意思是 C 标准库中的 fopen 调用(不是系统调用)
注意:Python v3 添加了许多附加模式。 link to docs
注意 ww+ 都可以做 The file is created if it does not exist
在 Windows 上,附加到模式的 b 以二进制模式打开文件,因此还有 rbwbr+b 等模式。 Windows 上的 Python 区分了文本文件和二进制文件;读取或写入数据时,文本文件中的行尾字符会自动稍作更改。
我是否可以说如果它是 awr+ 不会做一致独立的事情?还是我没有看到模式?模式是什么?
R
Renato Byrro

我注意到时不时地我需要重新搜索一遍谷歌 fopen,只是为了建立一个关于模式之间主要区别的心理形象。所以,我认为下次阅读图表会更快。也许其他人也会觉得这很有帮助。

https://d20p74l5mne5au.cloudfront.net/ExWNT-white-bg.png


所以 + 基本上意味着写作。这很奇怪,w 并不意味着它,但它意味着截断......(在阅读下一个答案之后,似乎 w 写入并且 a 代表附加。这更有意义......)如果文件不存在,您对文件创建有什么意见吗?
a 描述错误。写入始终位于末尾。
@而且我相信@Antti 指的是属性 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 位置是结束要强一些。
@CharlieParker 基本上有两个文件操作(读,写)。模式 r 主要用于读取,模式 w、a 主要用于写入。加号启用给定模式的第二个操作(简单地说)。
对于后代:截断意味着从头开始覆盖。
e
eglease

相同的信息,只是表格形式

                  | 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 - 打开文件后,初始位置设置为文件末尾

注意:aa+ 始终附加到文件末尾 - 忽略任何 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


为什么没有“如果文件不存在则创建文件。如果它存在,定位在开始,启用读写”?这对我来说是最明显的用例:我将数据存储在文件中。如果文件不存在,请创建它而不是出错。如果文件中有数据我想从顶部读取所有数据,请更新一些内容,然后在下一次加载它时从 0 完全重新写入文件。我使用 open(file,'a'); close(); open(file,'r+') 来完成此操作。
在这种情况下,“截断”是什么意思?
@CharlieParker 这意味着文件的所有内容都被擦除(文件为空)
您可能需要添加一个注释,即使用 aa+ 写入将始终发生在文件末尾,无论是否使用 seek() 手动移动指针。
更新表格以包含 Python 3 的“x”怎么样?
E
Eli Courtwright

选项与 C 标准库中的 fopen function 相同:

w 截断文件,覆盖已经存在的文件

a 附加到文件,添加到已经存在的任何内容

w+ 打开读取和写入,截断文件,但也允许您读回写入文件的内容

a+ 打开以进行附加和读取,允许您附加到文件并读取其内容


在这种情况下,“截断”是什么意思?如果有旧数据,是否意味着删除旧数据?或者其他更具体的东西?
@CharlieParker:正确 - 这意味着现有文件中的所有数据都将被删除,我们从现在空文件的开头开始写入。
P
Peilonrayz

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)。因此 rrt 相同。


“总是写到EOF”是什么意思?
@qff EOF 代表文件结束。 “总是”意味着即使您 seek(0)(将光标移动到文件的开头)然后当您 write("foo") 文件将写入文件末尾。因此,为什么它被称为“附加”模式。您总是附加到文件的末尾。
G
Goran B.

我认为这对于跨平台执行(即作为 CYA)来说很重要。 :)

在 Windows 上,附加到模式的 'b' 以二进制模式打开文件,因此还有 'rb'、'wb' 和 'r+b' 等模式。 Windows 上的 Python 区分了文本文件和二进制文件;读取或写入数据时,文本文件中的行尾字符会自动稍作更改。这种对文件数据的幕后修改适用于 ASCII 文本文件,但它会破坏 JPEG 或 EXE 文件中的二进制数据。在读写此类文件时要非常小心使用二进制模式。在 Unix 上,将“b”附加到模式并没有什么坏处,因此您可以独立于平台使用它来处理所有二进制文件。

这直接引用自 Python Software Foundation 2.7.x


W
Wyrmwood

我偶然发现了这个,试图弄清楚为什么你会使用模式“w+”而不是“w”。最后,我只是做了一些测试。我看不出模式“w+”有多大用途,因为在这两种情况下,文件都会被截断。但是,使用“w+”,您可以在写完后通过回溯来阅读。如果您尝试使用“w”进行任何读取,则会引发 IOError。不使用模式 'w+' 的搜索读取不会产生任何结果,因为文件指针将在您写入的位置之后。


N
NelsonGon

我发现重要的是要注意 python 3 定义的打开模式与此处对 Python 2 正确的答案不同。

Python 3 opening modes 是:

'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)

模式 rwxa 与模式修饰符 bt 结合使用。 + 是可选添加的,应避免使用 U

正如我发现的困难方法一样,在以文本模式打开文件时始终指定 t 是个好主意,因为 r 是标准 open() 函数中 rt 的别名,但 {5 } 在所有压缩模块的 open() 函数中(例如读取 *.bz2 文件时)。

因此打开文件的模式应该是:

rt / wt / xt / at 用于以文本模式读取/写入/创建/附加到文件和

rb / wb / xb / ab 用于以二进制模式读取/写入/创建/附加到文件。

像以前一样使用 +