python - 被python文件模式迷惑 "w+"

标签 python file io

来自 doc ,

Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file). Append 'b' to the mode to open the file in binary mode, on systems that differentiate between binary and text files; on systems that don’t have this distinction, adding the 'b' has no effect.

here

w+ : Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

但是,如何读取用 w+ 打开的文件?

最佳答案

以下是打开文件的不同模式的列表:

  • r

    Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.

  • rb

    Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.

  • r+

    Opens a file for both reading and writing. The file pointer will be at the beginning of the file.

  • rb+

    Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file.

  • w

    Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

  • wb

    Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

  • w+

    Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

  • wb+

    Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

  • 一个

    Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

  • ab

    Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

  • a+

    Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

  • ab+

    Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

关于python - 被python文件模式迷惑 "w+",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16208206/

相关文章:

python - 打印数据结构

java - 创建一个带有目录的java文件

git - 从单个分支获取文件的所有版本

java - 从 java 文件中读取 N 行?

windows - 为什么此路径无法在 PERL 中打开 Windows 文件?

c - 如何从 C 中的大量单词列表中读取随机字符串?

performance - mysqldump 大MyISAM表启动快突然变慢

python - 奇怪的 Python 导入问题 - 内置函数的相对导入?

python - 字符串创建 : Differences between two syntaxes ' ' and ""?

python - dump() 缺少 1 个必需的位置参数 : 'fp' in python json