reading the last line of a file

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Thu Sep 8 05:38:24 EDT 2005


Martin Franklin wrote:
> Martin Franklin wrote:
> 
>>Xah Lee wrote:
>>
>>
>>>Martin Franklin wrote:
>>>
>>>
>>>
>>>>import gzip
>>>>log_file = gzip.open("access_log.4.gz")
>>>>last_line = log_file.readlines()[-1]
>>>>log_file.close()
>>>
>>>
>>>does the
>>>log_file.readlines()[-1]
>>>actually read all the lines first?
>>
>>
>>
>>Yes I'm afraid it does.
>>
>>
>>
>>
>>>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.
>>>
>>
>>
>>Ok, in that case stick to your shell based solution, although 100
>>megabytes does not sound that large to me I guess it is relative
>>to the system you are running on :) (I have over a gig of memory here)
>>
> 
> 
> And just a few minutes after I sent that... this...
> 
> import gzip
> 
> logfile = gzip.open("access_log.4.BIG.gz")
> 
> ## seek relative to the end of the file
> logfile.seek(-500)
> 
> last_line = logfile.readlines()[-1]
> 
> logfile.close()
> 
> print last_line
> 
> 
> Works quite fast on my machine...
> 

whoops, no it doesn't looking at wrong window :( just ignore
me please :)





More information about the Python-list mailing list