reading the last line of a file

Leif K-Brooks eurleif at ecritters.biz
Thu Sep 8 07:23:13 EDT 2005


Xah Lee wrote:
> i switched to system call with tail because originally i was using a
> pure Python solution
> 
>          inF = gzip.GzipFile(ff, 'rb');
>          s=inF.readlines()
>          inF.close()
>          last_line=s[-1]
> 
> and since the log file is 100 megabytes it takes a long time and hogs
> massive memory.

How about:

inF = gzip.GzipFile(ff, 'rb')
for line in inF:
    pass
last_line = line

It's a bit slower than gzip and tail, but the memory usage is fine.



More information about the Python-list mailing list