How to find the beginning of last line of a big text file ?
google at mrabarnett.plus.com
Thu Jan 1 18:31:06 CET 2009
Barak, Ron wrote:
> I have a _very_ big text file: I need to find the place where the last
> line begins (namely, the offset of the one-before-the-last '\n' + 1).
> Could you suggest a way to do that without getting all the file into
> memory (as I said, it's a big file), or heaving to readline() all lines
> (ditto) ?
You could seek() to near the end of the file before reading lines with
readline(). Remember that the seek will almost certainly put the file
pointer somewhere in the middle of a line, but that doesn't matter
provided that it's not the last line (ie if the second readline()
returns "" then the first readline() started somewhere in middle of the
last line of the file). If you find that the seek put the file pointer
somewhere in the middle of the last line, then try again, but this time
seeking further back from the end of file before reading. Repeat as
More information about the Python-list