how to read the last line of a huge file???

Alan Meyer ameyer2 at
Tue Feb 1 04:42:46 CET 2011

On 01/26/2011 04:22 PM, MRAB wrote:
> On 26/01/2011 10:59, Xavier Heruacles wrote:
>> I have do some log processing which is usually huge. The length of each
>> line is variable. How can I get the last line?? Don't tell me to use
>> readlines or something like linecache...
> Seek to somewhere near the end and then read use readlines(). If you
> get fewer than 2 lines then you can't be sure that you have the entire
> last line, so seek a little farther from the end and try again.

I think this has got to be the most efficient solution.

You might get the source code for the open source UNIX utility "tail" 
and see how they do it.  It seems to work with equal speed no matter how 
large the file is and I suspect it uses MRAB's solution, but because 
it's written in C, it probably examines each character directly rather 
than calling a library routine like readlines.


More information about the Python-list mailing list