Difference between modes a, a+, w, w+, and r+ in built-in open function?

后端 未结 6 1592
梦毁少年i
梦毁少年i 2020-11-21 05:43

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

6条回答
  •  天涯浪人
    2020-11-21 06:07

    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

提交回复
热议问题