[Pythonmac-SIG] Read a file backwards using fileinput

Oliver Steele steele@cs.brandeis.edu
Sun, 13 Jun 1999 20:59:31 -0400


> is it possible to read a file beginning from the last-line up to the first
> one using the fileinput mechanism??

No; the source to fileinput contains this line:
        if i != self._lineno:
            raise RuntimeError, "accessing lines out of order"
that basically checks that you're accessing the lines sequentially.

http://www.cs.brandeis.edu/~steele/sources/textlines.py, that I mentioned in
my last message, lets you read the lines backwards, but it's dreadfully
inefficient.  (In this case, it reads from the beginning of the file each
time.)

If you have to read the lines backwards, and if it's too big to fit in
memory, your best bet is probably to traverse it forwards building an array
mapping line number to file offset (using file.tell() and file.readline()),
and then step through the array backwards retrieving each line (using
file.seek()).  If the array is too big, you can store every tenth, or
hundredth, offset, and read blocks of 100 lines and walk through them
backwards.

But maybe there's some other way to do what you're doing, that doesn't
involve walking through the file backwards?  (Maybe you can build whatever
data structure you're building from the information in the file, and then
walk through _it_ backwards?)