A bug with file.tell()?

Andrew Dalke adalke at mindspring.com
Fri Sep 10 09:11:27 CEST 2004

Nick Jacobson wrote:
> Is this a bug?

   [example of mixing iterator-style line reads from a
    file and tell]

No, it's a documented behaviour.


] In order to make a for loop the most efficient way
] of looping over the lines of a file (a very common
] operation), the next() method uses a hidden read-ahead
] buffer. As a consequence of using a read-ahead
] buffer, combining next() with other file methods (like
] readline()) does not work right. However, using seek()
] to reposition the file to an absolute position will
] flush the read-ahead buffer.

> This code involves the file methods "seek" and "tell".  Even though
> the file pointer is in the middle of the file, "tell" returns a
> position at the end of the file!

That means the underlying file handle is at
the end and everything remaining is in memory.

				dalke at dalkescientific.com

More information about the Python-list mailing list