Python open () append and read, file.read () returns empty string

Note strange behavior when trying to call read()

on a file opened in a+

(Python 3.4.1) mode

As seen here
File mode for create + read + append + binary file
It is possible to open a file in read / append mode ostensibly.

However
This code:

with open("hgrc", "a+") as hgrc:
            contents=hgrc.read()

      

returns contents={str}''

. This is surprising based on the answer above.
Now the following code

with open("hgrc", "r+") as hgrc:
            contents=hgrc.read()

      

returns contents={str}'contents of hgrc.....'

what is expected, but prevents us from adding a file.

According to specs

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

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.

What does it mean
When we open a file in mode a+

, we need to be able to name read()

on it and get the contents of the file back, right? Thoughts? Opinions? Etc ??

+3


source to share


2 answers


a+

opens the file at the end for adding. You have to call .seek(0)

on it if you want to read its contents, but at this point you can simply use r+

because that opens the file at the beginning.



+5


source


This is a problem with Python 2 and Python 3.

open()

c a+

behaves differently in the two Python versions. (Note: You are showing that you are using Python 3.4.1, but citing the documentation for Python 2!)

(Actually the behavior you're looking for (in "What Does") work as intended with Python 2. I think they changed the behavior because "append" means "file pointer at the end of the file" to a lot of people.)


Let's check it out with Python3 ...

$ 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 reset the file pointer ...
>>> lic.seek(0)
0
>>> lic.read()
'Apache License\nVersion 2.0, January 2004\n...'

      

It's the same with Python2 ...



$ 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: It's safe to use seek(0)

always after opening a file with a+

, no matter what version of Python you are using. It looks like a regime a+

.


Why does the system call behave differently in the two Python versions?

You might think that manipulating files is a system call, so it is handled by the operating system. This differs from Python in that it looks like Python documentation :

Note. Python is independent of the presentation of text files on the underlying operating systems; all processing is done by Python itself and is therefore platform independent.

BTW, this behavior has been reported as a bug in the Python bug tracker.

+3


source







All Articles