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

后端 未结 6 1588
梦毁少年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:08

    The options are the same as for the fopen function in the C standard library:

    w truncates the file, overwriting whatever was already there

    a appends to the file, adding onto whatever was already there

    w+ opens for reading and writing, truncating the file but also allowing you to read back what's been written to the file

    a+ opens for appending and reading, allowing you both to append to the file and also read its contents

提交回复
热议问题