python打开内置函数:模式A、A+、W、W+和R+之间的区别?

python open built-in function: difference between modes a, a+, w, w+, and r+?

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

特别是,文档意味着所有这些都将允许对文件进行写入,并表示它将打开文件进行"附加"、"写入"和"更新",但不定义这些术语的含义。


打开模式与C标准库函数fopen()的打开模式完全相同。

BSD fopen手册页定义如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 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,只是为了建立一个关于模式之间主要差异的心理图像。所以,我想下次看图表会更快。也许其他人也会觉得这很有用。

></P></p>
<div class=


同样的信息,只是以表格形式

1
2
3
4
5
6
7
8
9
                  | r   r+   w   w+   a   a+
------------------|--------------------------
read              | +   +        +        +
write             |     +    +   +    +   +
write after seek  |     +    +   +
create            |          +   +    +   +
truncate          |          +   +
position at start | +   +    +   +
position at end   |                   +   +

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

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

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

  • tructate-在打开文件时,该文件变为空(文件的所有内容都将被删除)

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

  • 结束位置-打开文件后,初始位置设置为文件结尾

注:aa+总是附加到文件结尾-忽略任何seek的移动。顺便说一句,有趣的行为至少在我的win7/python2.7上,对于在a+模式下打开的新文件:write('aa'); seek(0, 0); read(1); write('b')—忽略第二个writewrite('aa'); seek(0, 0); read(2); write('b')—第二个write提高IOError


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

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

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

w+打开进行读写,截断文件,同时允许您读取已写入文件的内容。

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.

这是直接从Python软件基金会2.7。


我偶然发现了这个问题,想弄明白为什么你会使用"w+"和"w"模式。最后,我做了一些测试。我看不出模式"w+"有多大用途,因为在这两种情况下,文件都被截断以开始。但是,使用"w+",您可以在写完后通过回访进行阅读。如果您尝试使用"w"进行任何读取,它将引发一个ioerror。不使用模式"w+"的SEEK进行读取不会产生任何结果,因为文件指针将位于您写入的位置之后。