Python 3.2 bug? Reading the last line of a file

MRAB python at mrabarnett.plus.com
Wed May 25 16:00:18 EDT 2011


On 25/05/2011 20:33, tkpmep at hotmail.com wrote:
> The following function that returns the last line of a file works
> perfectly well under Python 2.71. but fails reliably under Python 3.2.
> Is this a bug, or am I doing something wrong? Any help would be
> greatly appreciated.
>
>
> import os
>
> def lastLine(filename):
>      '''
>          Returns the last line of a file
>          file.seek takes an optional 'whence' argument which allows you
> to
>          start looking at the end, so you can just work back from there
> till
>          you hit the first newline that has anything after it
>          Works perfectly under Python 2.7, but not under 3.2!
>     '''
>      offset = -50
>      with open(filename) as f:
>          while offset>  -1024:
>              offset *= 2
>              f.seek(offset, os.SEEK_END)
>              lines = f.readlines()
>              if len(lines)>  1:
>                  return lines[-1]
>
> If I execute this with a valid filename fn. I get the following error
> message:
>
>>>> lastLine(fn)
> Traceback (most recent call last):
>    File "<pyshell#12>", line 1, in<module>
>      lastLine(fn)
>    File "<pyshell#11>", line 13, in lastLine
>      f.seek(offset, os.SEEK_END)
> io.UnsupportedOperation: can't do nonzero end-relative seeks
>
You're opening the file in text mode, and seeking relative to the end
of the file is not allowed in text mode, presumably because the file
contents have to be decoded, and, in general, seeking to an arbitrary
position within a sequence of encoded bytes can have undefined results
when you attempt to decode to Unicode starting from that position.

The strange thing is that you _are_ allowed to seek relative to the
start of the file.

Try opening the file in binary mode and do the decoding yourself,
catching the DecodeError exceptions if/when they occur.



More information about the Python-list mailing list