一尘不染

python打开内置函数:模式a,a +,w,w +和r +之间的区别?

python

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

特别是,文档暗示所有这些都将允许写入文件,并表示它打开文件专门用于"appending", "writing", and "updating“,但未定义这些术语的含义。


阅读 421

收藏
2020-02-08

共1个答案

一尘不染

打开模式与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 
2020-02-08