【问题标题】:Difference between modes a, a+, w, w+, and r+ in built-in open function?内置 open 函数中模式 a、a+、w、w+ 和 r+ 的区别?
【发布时间】:2010-11-30 18:16:48
【问题描述】:

在python内置的open函数中,waw+a+r+模式的具体区别是什么?

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

【问题讨论】:

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

标签: python


【解决方案1】:

打开方式与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,我可以说+ 不会做一致独立的事情吗?还是我没有看到模式?模式是什么?
【解决方案2】:

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

【讨论】:

  • 所以+ 基本上意味着写作。这很奇怪,w 并不意味着它,而是意味着截断......(在阅读下一个答案之后,似乎w 写完了,a 代表追加。这更有意义......)如果文件不存在,您是否有任何关于文件创建的 cmets?
  • 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 属性,这比仅仅说初始 位置是结束要强一些。
  • @CharlieParker 基本上有两种文件操作(读、写)。 r 模式主要用于读取,wa 模式主要用于写入。 加号为给定模式启用第二个操作(简单地说)。
  • 对于后代:截断意味着从头开始覆盖。
【解决方案3】:

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

                  | r   r+   w   w+   a   a+
------------------|--------------------------
read              | +   +        +        +
write             |     +    +   +    +   +
write after seek  |     +    +   +
create            |          +   +    +   +
truncate          |          +   +
position at start | +   +    +   +
position at end   |                   +   +

其中的含义是: (只是为了避免任何误解)

  • 读取 - 允许从文件中读取
  • 写入 - 允许写入文件

  • create - 如果文件尚不存在则创建文件

  • 截断 - 在打开文件期间将其设为空(文件的所有内容都被删除)

  • 开始位置 - 打开文件后,初始位置设置为文件的开头

  • position at end - 打开文件后,初始位置设置为文件末尾

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

【讨论】:

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

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

w 截断文件,覆盖已经存在的所有内容

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

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

a+ 为追加和读取打开,允许您追加到文件并读取其内容

【讨论】:

  • 在这种情况下,“截断”是什么意思?如果有旧数据,是否意味着删除旧数据?或者其他更具体的东西?
  • @CharlieParker:正确 - 这意味着现有文件中的所有数据都将被删除,我们从现在为空的文件的开头开始写入。
【解决方案5】:
r r+ x x+ w w+ a a+
readable x x x x x
writeable x x x x x x x
default position: start x x x x x x
default position: end x x
must exist x x
mustn't exist x x
truncate (clear file) on load x x
Always write to EOF x x

模式

t (default) b
str (io.TextIOBase) x
bytes (io.BufferedIOBase) x

如果没有选择模式;使用文本模式 (t)。因此rrt 相同。

【讨论】:

  • “总是写到 EOF”是什么意思?
  • @qff EOF 代表文件结束。 “总是”意味着即使您 seek(0)(将光标移动到文件的开头)然后当您 write("foo") 时,文件将写入文件末尾。因此,为什么它被称为“附加”模式。你总是追加到文件的末尾。
【解决方案6】:

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

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

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

【讨论】:

    【解决方案7】:

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

    【讨论】:

      【解决方案8】:

      我发现需要注意的是,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 是标准rt 函数中rt 的别名,但在标准open() 函数中是别名用于所有压缩模块的open() 函数中的rb(例如读取*.bz2 文件时)。

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

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

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

      像以前一样使用+

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-02
        • 2012-12-31
        • 2016-08-15
        相关资源
        最近更新 更多