首页 文章

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

提问于
浏览
445

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

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

6 回答

  • 7

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

    我注意到,我不时需要Google重新开始,只是为了 Build 模式之间主要差异的心理形象 . 所以,我认为下一次阅读图表会更快 . 也许其他人也会觉得有帮助 .

  • 36

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

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

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

    • 读取 - 允许从文件读取

    • 写入 - 允许写入文件

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

    • trunctate - 在打开文件期间,它变为空(文件的所有内容都被删除)

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

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

    注意: aa+ 总是附加到文件末尾 - 忽略任何 seek 动作 .
    BTW . 对于在 a+ 模式下打开的新文件,至少在我的win7 / python2.7上有趣的行为:
    write('aa'); seek(0, 0); read(1); write('b') - 第二个 write 被忽略
    write('aa'); seek(0, 0); read(2); write('b') - 第二次 write 加注 IOError

  • 7

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

    w 截断文件,覆盖已有的文件

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

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

    a+ 打开以进行追加和读取,允许您同时附加到文件并读取其内容

  • 137

    我试图弄清楚为什么你会使用模式'w'而不是'w' . 最后,我做了一些测试 . 我没有看到模式'w'的目的太多,因为在这两种情况下,文件都被截断为开头 . 然而,有了'w',你可以在写完之后回头看 . 如果您尝试使用'w'进行任何读取,则会引发IOError . 不使用带有'w'模式的搜索进行读取不会产生任何结果,因为文件指针将位于您编写的位置之后 .

  • 552

    我认为这对跨平台执行很重要,即作为CYA . :)

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

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

相关问题