reading the last line of a file

Xah Lee xah at xahlee.org
Thu Sep 8 10:36:31 CEST 2005


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?

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.

 Xah
 xah at xahlee.orghttp://xahlee.org/




More information about the Python-list mailing list