how to read the last line of a huge file???
Roy Smith
roy at panix.com
Thu Jan 27 08:55:21 EST 2011
In article <mailman.1353.1296119065.6505.python-list at python.org>,
Alice Bevan–McGregor <alice at gothcandy.com> wrote:
> On 2011-01-26 02:59:26 -0800, Xavier Heruacles said:
>
> > I have do some log processing which is usually huge. The length of each
> > line is variable. How can I get the last line?? Don't tell me to use
> > readlines or something like linecache...
>
> This is not optimum or efficient, but it works! If you want to see
> what's going on, use 4 instead of 4096 and 8 instead of 8192 and add a
> print statement to the bottom of the while loop. :)
>
> import os
>
> with open('biglogfile.log', 'r') as fh:
> fh.seek(-4096, os.SEEK_END)
> buffer = fh.read(4096)
>
> # We are expecting a trailing newline.
> while "\n" not in buffer[:-1]:
> fh.seek(-8192, os.SEEK_CUR)
> buffer = fh.read(4096) + buffer
>
> # Eliminate empty lines, they aren't useful.
> lines = [line for line in buffer.split('\n') if line]
> print lines[-1]
>
> — Alice. :)
Back in the old days, if you wanted to read a file backwards, you just
used a tape drive that had read-reverse capability :-)
More information about the Python-list
mailing list