File: How to read the last line?

Guido van Rossum guido at eric.cnri.reston.va.us
Thu May 6 08:20:05 EDT 1999


"Darrell" <news at dorb.com> writes:

> A small variation on Bowman's idea. But a problem is what is the max length
> of a line ? Working in SGML a line can be huge.
> 
> fp=open('xxx.sgm')
> fp.seek(-1024,2)
> l=fp.readlines()
> print l[-1]

Have a look at Tools/scripts/rgrep.py in the Python source
distribution; it's a reverse grep (i.e. it searches from the end of
the file backwards) that deals with this, including what to do if the
last line is really big.

--Guido van Rossum (home page: http://www.python.org/~guido/)




More information about the Python-list mailing list