内置开放功能中的a,a +,w,w +和r +模式之间的区别?

问题:内置开放功能中的a,a +,w,w +和r +模式之间的区别?

在内置的Python开放的功能,是个什么模式之间准确的区别waw+a+,和r+

特别地,文档暗示所有这些都将允许写入文件,并说它打开文件专门用于“追加”,“写入”和“更新”,但未定义这些术语的含义。

In the python built-in open function, what is the exact difference between the modes w, a, w+, a+, and r+?

In particular, the documentation implies that all of these will allow writing to the file, and says that it opens the files for “appending”, “writing”, and “updating” specifically, but does not define what these terms mean.


回答 0

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

The opening modes are exactly the same as those for the C standard library function fopen().

The BSD fopen manpage defines them as follows:

 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.

回答 1

我注意到,我不时需要重新打开Goog​​le,只是为了构想两种模式之间的主要区别是什么。因此,我认为下一次阅读图会更快。也许其他人也会发现它也有帮助。

I noticed that every now and then I need to Google fopen all over again, just to build a mental image of what the primary differences between the modes are. So, I thought a diagram will be faster to read next time. Maybe someone else will find that helpful too.


回答 2

相同信息,只是表格形式

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

意义在哪里:(为避免任何误解)

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

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

  • 截断-在打开文件期间将其清空(删除了文件的所有内容)

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

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

注意: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

Same info, just in table form

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

where meanings are: (just to avoid any misinterpretation)

  • read – reading from file is allowed
  • write – writing to file is allowed

  • create – file is created if it does not exist yet

  • trunctate – during opening of the file it is made empty (all content of the file is erased)

  • position at start – after file is opened, initial position is set to the start of the file

  • position at end – after file is opened, initial position is set to the end of the file

Note: a and a+ always append to the end of file – ignores any seek movements.
BTW. interesting behavior at least on my win7 / python2.7, for new file opened in a+ mode:
write('aa'); seek(0, 0); read(1); write('b') – second write is ignored
write('aa'); seek(0, 0); read(2); write('b') – second write raises IOError


回答 3

这些选项与C标准库中的fopen函数相同:

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

a 追加到文件,添加到已经存在的文件中

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

a+ 打开以进行追加和读取,使您既可以追加到文件,也可以读取其内容

The options are the same as for the fopen function in the C standard library:

w truncates the file, overwriting whatever was already there

a appends to the file, adding onto whatever was already there

w+ opens for reading and writing, truncating the file but also allowing you to read back what’s been written to the file

a+ opens for appending and reading, allowing you both to append to the file and also read its contents


回答 4

我认为对于跨平台执行(即作为CYA),考虑这一点很重要。:)

在Windows上,附加到模式的’b’以二进制模式打开文件,因此也有’rb’,’wb’和’r + b’之类的模式。Windows上的Python区分文本文件和二进制文件。当读取或写入数据时,文本文件中的行尾字符会自动更改。对于ASCII文本文件,对文件数据进行这种幕后修改是可以的,但它会破坏JPEG或EXE文件中的二进制数据。读写此类文件时,请务必小心使用二进制模式。在Unix上,将’b’附加到该模式没有什么坏处,因此您可以独立于平台将其用于所有二进制文件。

直接从Python Software Foundation 2.7.x引用。

I think this is important to consider for cross-platform execution, i.e. as a CYA. :)

On Windows, ‘b’ appended to the mode opens the file in binary mode, so there are also modes like ‘rb’, ‘wb’, and ‘r+b’. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files. On Unix, it doesn’t hurt to append a ‘b’ to the mode, so you can use it platform-independently for all binary files.

This is directly quoted from Python Software Foundation 2.7.x.


回答 5

我碰巧试图弄清楚为什么要使用模式“ w +”与“ w”。最后,我只是做了一些测试。我看不到’w +’模式有什么用,因为在两种情况下,文件都是从头开始被截断的。但是,有了“ w +”,您可以在写完后通过回头阅读。如果您尝试使用“ w”进行任何读取,则将引发IOError。在模式’w +’下不使用seek进行读取不会产生任何结果,因为文件指针将位于您写入的位置之后。

I hit upon this trying to figure out why you would use mode ‘w+’ versus ‘w’. In the end, I just did some testing. I don’t see much purpose for mode ‘w+’, as in both cases, the file is truncated to begin with. However, with the ‘w+’, you could read after writing by seeking back. If you tried any reading with ‘w’, it would raise an IOError. Reading without using seek with mode ‘w+’ isn’t going to yield anything, since the file pointer will be after where you have written.