Read from the last line of a file?

Scott David Daniels Scott.Daniels at Acm.Org
Wed Mar 23 08:19:31 EST 2005


Mike Rovner wrote:
> Anthony Liu wrote:
>> I am wondering if it is possible to start reading from
>> the last line of file, and then the last but one up to
>> the first line.
> If you can afford to keep the whole file in memory, than:
>     lines = open(..).readlines()
>     print lines[::-1]

Or use the new "reversed" generator to avoid a copy.

     for line in reversed(....readlines()):
         ....

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list