File Reading , Reverse Direction
Peter Hansen
peter at engcorp.com
Fri Jul 27 21:36:37 EDT 2001
Kannan Vijayan wrote:
>
> A modification of Peter Hansen's code:
>
> i = -4096
> file.seek(i, 2)
> lines = file.readlines()
> while len(lines) <= 2:
> i -= 4096
> file.seek(i, 2)
> lines = file.readlines()
> lines = lines[-2:]
Just be sure to catch the case where you try to seek() to
an offset before the start of the file: it throws an
IOError. And the original poster was probably counting
on not having files with fewer than two lines... :)
--
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com
More information about the Python-list
mailing list