A bug with file.tell()?

Andrew Dalke adalke at mindspring.com
Fri Sep 10 03:11:27 EDT 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.

http://docs.python.org/lib/bltin-file-objects.html

] 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.



				Andrew
				dalke at dalkescientific.com



More information about the Python-list mailing list