Noticed an odd behavior when attempting to call read()
on a file opened in a+
mode (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}''
. 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.
According to the 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.
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?
Thoughts? Opinions? Etc??