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

发布于 2021-02-02 23:23:35

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

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

关注者
0
被浏览
177
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

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


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看