Skip to content Skip to sidebar Skip to footer

Calling Tell() On Python File Objects Opened In Append Mode Returns 0 If Not Manually Seeking To Eof

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

Solution 1:

Under Python 2, append mode means every write goes to the end of the file. The file pointer might really start at zero, but that doesn't matter because it will seek to the end every time you write.

Note that seek() and tell() are largely useless in append mode, since the former will always be overridden by the implicit seek-to-end. If you need to append to the file without this behavior, open it in r+b mode and then manually seek to the end of the file.

Post a Comment for "Calling Tell() On Python File Objects Opened In Append Mode Returns 0 If Not Manually Seeking To Eof"