Python file.tell() giving strange numbers?

It’s a documented behaviour caused by UNIX-style line endings:

file.tell()

Return the file’s current position, like stdio‘s ftell().

Note: On Windows, tell() can return illegal values (after an fgets()) when reading files with Unix-style line-endings. Use binary mode
(‘rb’) to circumvent this problem.


The above documentation is taken from the python2.7.4 documentation. The documentation for python3 changed a bit, since there is now a hierarchy of classes that handle I/O and I can’t find this bit of information. Your test shows that the behaviour didn’t change anyway. Also the source code for python3.3 has an XXX Windows support below is likely incomplete comment before the function called by tell.


There is an issue in python bug tracker related to this, and the final comment by Catalin Iacob is:

I tried to reproduce this, picked a file on my disk and indeed I got a
negative number, but that file has Unix line endings. This is
documented at http://docs.python.org/2/library/stdtypes.html#file.tell
so probably there’s nothing to do then.

As for Armin’s report in msg180145, even though it’s not intuitive,
this matches ftell’s behavior on Windows, as documented in the Remarks
section of
http://msdn.microsoft.com/en-us/library/0ys3hc0b%28v=vs.100%29.aspx.
The tell() method on fileobjects is explicitly documented as matching
ftell behavior: “Return the file’s current position, like stdio‘s
ftell()”. So even though it’s not intuitive at all, it’s probably
better to leave it as is. tell() returns the intuitive non zero
position when opening with ‘a’ on Python3 and on Python 2.7 when using
io.open so it’s fixed for the future anyway.

So it seems like a “wontfix” bug. Someone should probably open an issue(commented the issue) because this fact is not mentioned at all in python3 documentation.


According to Antoine Pitrou python3 doesn’t use ftell() at all, hence this seems to be a different bug. Also the bug is not reproducible in python3.2.3 and was probably introduced when fixing this issue (at least, it’s the only change I can find to the implementation of tell() between 3.2.3 and 3.3)


Last edit: According to the io module documentation the tell method does not return the number of bytes since the beginning of a file. The returned value is an “opaque number”, which means that the only way you can use it is to pass it to seek to get back at that position. Other operations aren’t meaningful. The fact that until python3.2.3 the value returned was what you’d expect was only an implementation detail.

Note that the information in this section of the documentation is simply wrong and, hopefully, it will be fixed in the future.

Leave a Comment