在内置的蟒蛇开放的功能,是个什么模式之间准确的区别w
,a
,w+
,a+
,和r+
?
特别是,文档暗示所有这些都允许写入文件,并说它打开文件"具体"附加",写入"和"更新",但没有定义这些术语的含义.
drA*_*erT 674
打开模式与C标准库函数完全相同fopen()
.
BSD手册fopen
页将它们定义如下:
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.
Run Code Online (Sandbox Code Playgroud)
And*_*bis 449
我注意到,我不时需要谷歌重新开始,只是为了建立一个心理图像,了解模式之间的主要区别.所以,我认为下一次阅读图表会更快.也许其他人也会觉得有帮助.
ind*_*112 186
相同的信息,只是表格形式
| r r+ w w+ a a+
------------------|--------------------------
read | + + + +
write | + + + + +
write after seek | + + +
create | + + + +
truncate | + +
position at start | + + + +
position at end | + +
Run Code Online (Sandbox Code Playgroud)
含义是:(只是为了避免任何误解)
写 - 允许写入文件
如果文件尚不存在,则创建文件
trunctate - 在打开文件期间,它变为空(文件的所有内容都被删除)
开始时的位置 - 文件打开后,初始位置设置为文件的开头
注意:a
并且a+
始终附加到文件末尾 - 忽略任何seek
移动.
BTW.至少在我的win7/python2.7上有趣的行为,对于在a+
模式下打开的新文件:
write('aa'); seek(0, 0); read(1); write('b')
- 第二个write
被忽略
write('aa'); seek(0, 0); read(2); write('b')
- 第二次write
加注IOError
Pei*_*ayz 40
r | r+ | X | x+ | w | 瓦+ | A | 一个+ | |
---|---|---|---|---|---|---|---|---|
可读的 | \xe2\x88\x9a | \xe2\x88\x9a | \xe2\x88\x9a | \xe2\x88\x9a | \xe2\x88\x9a | |||
可写的 | \xe2\x88\x9a | \xe2\x88\x9a | \xe2\x88\x9a | \xe2\x88\x9a | \xe2\x88\x9a | \xe2\x88\x9a | \xe2\x88\x9a | |
默认位置:开始 | \xe2\x88\x9a | \xe2\x88\x9a | \xe2\x88\x9a | \xe2\x88\x9a | \xe2\x88\x9a | \xe2\x88\x9a | ||
默认位置:结束 | \xe2\x88\x9a | \xe2\x88\x9a | ||||||
必须存在 | \xe2\x88\x9a | \xe2\x88\x9a | ||||||
一定不存在 | \xe2\x88\x9a | \xe2\x88\x9a | ||||||
加载时截断(清除文件) | \xe2\x88\x9a | \xe2\x88\x9a | ||||||
始终写入 EOF | \xe2\x88\x9a | \xe2\x88\x9a |
模式
\nt(默认) | 乙 | |
---|---|---|
str ( io.TextIOBase ) | \xe2\x88\x9a | |
bytes ( io.BufferedIOBase ) | \xe2\x88\x9a |
如果没有选择模式;t
使用文本模式 ( )。因此r
与 相同rt
。
ing*_*yer 10
我发现需要注意的是,Python 3 定义的打开模式与此处针对 Python 2 的正确答案不同。
Python 3的开启模式有:
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists
----
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newlines mode (for backwards compatibility; should not be used in new code)
Run Code Online (Sandbox Code Playgroud)
模式r
, w
, x
,与模式修饰符或a
组合。是可选添加的,应该避免。b
t
+
U
正如我发现的那样,在t
以文本模式打开文件时始终指定是一个好主意,因为是标准函数中的r
别名,但是所有压缩模块函数中的别名(例如读取文件时) 。rt
open()
rb
open()
*.bz2
因此打开文件的模式应该是:
rt
///用于以文本模式读取/写入wt
/创建/附加到文件xt
at
rb
///用于以二进制模式读取/写入wb
/创建/附加到文件。xb
ab
像以前一样使用+
。
我试图弄清楚为什么你会使用'w +'模式和'w'模式.最后,我做了一些测试.我没有看到模式'w +'的目的太多,因为在这两种情况下,文件都被截断为开头.但是,使用'w +',您可以在写完之后通过回复来阅读.如果您尝试使用'w'进行任何读取,则会引发IOError.不使用模式'w +'进行读取不会产生任何结果,因为文件指针将位于您编写的位置之后.
我认为这对跨平台执行很重要,即作为CYA.:)
在Windows上,附加到模式的'b'以二进制模式打开文件,因此还有'rb','wb'和'r + b'等模式.Windows上的Python区分了文本和二进制文件; 读取或写入数据时,文本文件中的行尾字符会自动稍微改变.这种对文件数据的幕后修改适用于ASCII文本文件,但它会破坏像JPEG或EXE文件中的二进制数据.在读取和写入此类文件时要非常小心地使用二进制模式.在Unix上,将'b'附加到模式没有什么坏处,因此您可以独立于平台使用它来处理所有二进制文件.
这是直接转引自Python软件基金会2.7.x.
归档时间: |
|
查看次数: |
363525 次 |
最近记录: |