Difference between modes a, a+, w, w+, and r+ in built-in open function?(内置 open 函数中模式 a、a+、w、w+ 和 r+ 的区别?)
问题描述
在python内置的open函数中,具体有什么区别在 w
、a
、w+
、a+
和 r+
模式之间?
In the python built-in open function, what is the exact difference between the modes w
, a
, w+
, a+
, and r+
?
特别是,文档暗示所有这些都将允许写入文件,并说它打开文件是为了附加"、写入"和更新",但没有定义这些术语的含义.
In particular, the documentation implies that all of these will allow writing to the file, and says that it opens the files for "appending", "writing", and "updating" specifically, but does not define what these terms mean.
推荐答案
打开方式与C标准库函数fopen()
完全相同.
The opening modes are exactly the same as those for the C standard library function fopen()
.
BSD fopen
手册页 将它们定义如下:
The BSD fopen
manpage defines them as follows:
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.
这篇关于内置 open 函数中模式 a、a+、w、w+ 和 r+ 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:内置 open 函数中模式 a、a+、w、w+ 和 r+ 的区别?
基础教程推荐
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01