[英]Python open() append and read, file.read() returns empty string
Noticed an odd behavior when attempting to call read()
on a file opened in a+
mode (Python 3.4.1)尝试对以
a+
模式打开的文件调用read()
时注意到一个奇怪的行为(Python 3.4.1)
As seen here如这里所见
File mode for creating+reading+appending+binary 创建+读取+追加+二进制的文件模式
It's possible to open a file in read/append mode supposedly .这是可以打开的读/追加模式文件推测。
However然而
This code:这段代码:
with open("hgrc", "a+") as hgrc:
contents=hgrc.read()
returns contents={str}''
.返回
contents={str}''
。 Which is unexpected based upon the answer posted above.根据上面发布的答案,这是出乎意料的。
Now, the following code现在,下面的代码
with open("hgrc", "r+") as hgrc:
contents=hgrc.read()
returns contents={str}'contents of hgrc.....'
, which is expected, but doesn't give us the option to append to the file.返回
contents={str}'contents of hgrc.....'
,这是预期的,但没有给我们附加到文件的选项。
According to the specs根据规格
https://docs.python.org/2/library/functions.html#open https://docs.python.org/2/library/functions.html#open
Modes 'r+', 'w+' and 'a+' open the file for updating (reading and writing);
note that 'w+' truncates the file. Append 'b' to the mode to open the file
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
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
in binary mode, on systems that differentiate between binary and text
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
note that 'w+' truncates the file. Append 'b' to the mode to open the file
files; on systems that don't have this distinction, adding the 'b' has no
files; on systems that don't have this distinction, adding the 'b' has no
effect.
files; on systems that don't have this distinction, adding the 'b' has no
effect.
Which means意思是
When we open a file in a+
mode, we should be able to call read()
on it and get the contents of the file back, correct?当我们以
a+
模式打开文件时,我们应该能够对其调用read()
并取回文件的内容,对吗? Thoughts?想法? Opinions?
意见? Etc??
等等??
This is a Python 2 vs. Python 3 issue.这是 Python 2 与 Python 3 的问题。
open()
with a+
behaves differently in the two Python versions. open()
with a+
在两个 Python 版本中的行为不同。 (Note: You reveal you're using Python 3.4.1 , but you're quoting the documentation for Python 2 !) (注意:您透露您使用的是 Python 3.4.1 ,但您引用的是 Python 2的文档!)
(In fact, the behavior you're looking for (in " Which means ") works as intended with Python 2. I think they changed the behavior, because "appending" means "file pointer at end of file" to many folks.) (实际上,您正在寻找的行为(在“这意味着”中)在 Python 2 中按预期工作。我认为他们改变了行为,因为“追加”对许多人来说意味着“文件末尾的文件指针”。)
$ python3
Python 3.4.3 (default, Jul 28 2015, 18:20:59)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> lic = open('LICENSE', 'a+')
>>> lic.read()
''
>>> # Hmmm, no content? EOF, obviously. Let's reset the file pointer ...
>>> lic.seek(0)
0
>>> lic.read()
'Apache License\nVersion 2.0, January 2004\n...'
$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> lic = open('LICENSE', 'a+')
>>> lic.read()
'Apache License\nVersion 2.0, January 2004\n...'
>>> lic.seek(0)
>>> lic.read()
'Apache License\nVersion 2.0, January 2004\n...'
Conclusion: You're safe using the seek(0)
always after opening a file with a+
, regardless to which Python version you use.结论:在使用
a+
打开文件后始终使用seek(0)
是安全的,无论您使用哪个 Python 版本。 This seems to be specific to the a+
mode.这似乎是特定于
a+
模式的。
One would think file manipulation is a system call, hence it's handled by the operating system.有人会认为文件操作是一种系统调用,因此它由操作系统处理。 This is different with Python, as it looks according to the Python documentation :
这与 Python 不同,它根据Python 文档显示:
Note: Python doesn't depend on the underlying operating system's notion of text files;
注意: Python 不依赖于底层操作系统的文本文件概念; all the processing is done by Python itself, and is therefore platform-independent.
所有处理都由 Python 本身完成,因此与平台无关。
BTW, this behavior has been reported as a bug on the Python bug tracker.顺便说一句,此行为已被报告为Python 错误跟踪器上的错误。
a+
opens the file at the end for appending. a+
在末尾打开文件以进行追加。 You need to call .seek(0)
on it if you want to then read in its contents, but at that point you might as well just use r+
, because that opens the file at the start.如果您想读取其内容,则需要对其调用
.seek(0)
,但此时您最好只使用r+
,因为这会在开始时打开文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.