Barak, Ron wrote:
Hi,
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
necessary.
--
http://mail.python.org/mailman/listinfo/python-list