I noticed a strange behavior of Python file handlers, when created in append mode. In the following example, ofh.tell() returns 0 the first time i use it in append mode.
ofh = open("test.txt","wb")
print ofh.tell() # output: 0
ofh.write("asdf")
print ofh.tell() # output: 4
ofh.close()
ofh = open("test.txt","ab")
print ofh.tell() # output: 0
ofh.seek(0,2) # seek to end of file
print ofh.tell() # output: 4
For some safety checks, i need it to return the "actual" position (4) in this case.
- Is manually seeking to the end of the file the way to go?
- Is there any good reason for the observed behavior? Returning a position different from where data will be written doesn't seem like a good idea.
I am using Python 2.7.10 and Windows 7 64bit.