Read from the last line of a file?
Mike Rovner
mrovner at propel.com
Tue Mar 22 22:51:28 EST 2005
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]
Otherwise you can use seek() to random-access file and read chunks,
then extract lines from them and process them.
See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/120686 for
implementation details.
Mike
More information about the Python-list
mailing list