How to read lines only at the end of file?

Batista, Facundo FBatista at uniFON.com.ar
Fri Nov 7 10:13:19 EST 2003


Alex Martelli wrote:

#- Yes: f.readlines() does read all of the file.
#- 
#- You can use f.seek to position yourself at some distance
#- from the _end_ (in bytes, though) and .readlines() from there --
#- but if the last N lines are abnormally long you're still not
#- guaranteed to be able to give the last N lines without reading
#- all of the file, worst case.

Maybe the best solution is, from the end of the file (with f.seek()), start
counting the end-of-lines backwards.

When you reach the n-th end-of-line, from there to the end of the file you
have what you want.

Of course, it'll best to read chunk of 512 bytes or something.

Or, you can go and study the code of "tail" (open sources gives you freedom,
:)

.	Facundo





More information about the Python-list mailing list