how to read the last line of a huge file???

MRAB python at mrabarnett.plus.com
Fri Mar 4 19:26:40 EST 2011


On 04/03/2011 21:46, tkpmep at hotmail.com wrote:
> I've implementing this method of reading a file from the end, i.e
>
> def seeker(filename):
>      offset = -10
>      with open(filename) as f:
>          while True:
>              f.seek(offset, os.SEEK_END)
>              lines = f.readlines()
>              if len(lines)>= 2:
>                  return lines[-1]
>              offset *= 2
>
> and consistently run into the following error message when Python 3.2
> (running under Pyscripter 2.4.1) tries to execute the line
> f.seek(offset,2)
>
> UnsupportedOperation: can't do non-zero end-relative seeks
>
> But offset is initialized to -10. Does anyone have any thoughts on
> what the error might be caused by?
>
I think it's because the file has been opened in text mode, so there's
the encoding to consider. It may be that it's to stop you from
accidentally seeking into the middle of a multibyte sequence, but
there's nothing to stop you doing that when seeking relative to the
start, for example, so it's possibly a pointless restriction.

A workaround is not to seek relative to the end. os.path.getsize() will
tell you the length of the file. You'll still have to watch out for
DecodeError when you read in case the seek was into the middle of a
multibyte sequence. A better workaround may be to open in binary mode
and decode the bytes explicitly; if there's a DecodeError then discard
the first byte and try again, etc.



More information about the Python-list mailing list