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

Alice Bevan–McGregor alice at gothcandy.com
Thu Jan 27 04:04:07 EST 2011


On 2011-01-26 02:59:26 -0800, Xavier Heruacles said:

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

This is not optimum or efficient, but it works!  If you want to see 
what's going on, use 4 instead of 4096 and 8 instead of 8192 and add a 
print statement to the bottom of the while loop.  :)

import os

with open('biglogfile.log', 'r') as fh:
    fh.seek(-4096, os.SEEK_END)
    buffer = fh.read(4096)

    # We are expecting a trailing newline.
    while "\n" not in buffer[:-1]:
        fh.seek(-8192, os.SEEK_CUR)
        buffer = fh.read(4096) + buffer

    # Eliminate empty lines, they aren't useful.
    lines = [line for line in buffer.split('\n') if line]
    print lines[-1]

	— Alice.  :)





More information about the Python-list mailing list