Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6261

Re: Python 3.2 bug? Reading the last line of a file

Date 2011-05-25 21:00 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: Python 3.2 bug? Reading the last line of a file
References <3d81e2a0-6c86-4f12-a1c4-ce4c736172b6@y31g2000vbp.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2091.1306353619.9059.python-list@python.org> (permalink)

Show all headers | View raw


On 25/05/2011 20:33, tkpmep@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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python 3.2 bug? Reading the last line of a file "tkpmep@hotmail.com" <tkpmep@hotmail.com> - 2011-05-25 12:33 -0700
  Re: Python 3.2 bug? Reading the last line of a file MRAB <python@mrabarnett.plus.com> - 2011-05-25 21:00 +0100
  Re: Python 3.2 bug? Reading the last line of a file Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-25 14:54 -0600
  Re: Python 3.2 bug? Reading the last line of a file MRAB <python@mrabarnett.plus.com> - 2011-05-25 22:52 +0100
    Re: Python 3.2 bug? Reading the last line of a file "tkpmep@hotmail.com" <tkpmep@hotmail.com> - 2011-05-25 16:25 -0700
      Re: Python 3.2 bug? Reading the last line of a file Ethan Furman <ethan@stoneleaf.us> - 2011-05-25 16:58 -0700
      Re: Python 3.2 bug? Reading the last line of a file MRAB <python@mrabarnett.plus.com> - 2011-05-26 00:56 +0100
      Re: Python 3.2 bug? Reading the last line of a file Ethan Furman <ethan@stoneleaf.us> - 2011-05-25 17:32 -0700
      Re: Python 3.2 bug? Reading the last line of a file Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2011-05-26 08:09 +0300
        Re: Python 3.2 bug? Reading the last line of a file "tkpmep@hotmail.com" <tkpmep@hotmail.com> - 2011-05-27 12:21 -0700
  Re: Python 3.2 bug? Reading the last line of a file Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-25 19:06 -0600

csiph-web