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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:bug': 0.04; 'encoded': 0.05; 'subject:Python': 0.06; 'end,': 0.07; 'mode,': 0.07; 'python': 0.08; "'''": 0.09; '>>>>': 0.09; 'bug,': 0.09; 'exceptions': 0.09; 'filename': 0.09; 'from:addr:python': 0.09; 'newline': 0.09; 'undefined': 0.09; 'def': 0.12; 'binary': 0.14; 'subject:file': 0.14; 'wrote:': 0.14; '3.2.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'nonzero': 0.16; 'occur.': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reply-to:addr:python-list': 0.16; 'argument': 0.16; 'traceback': 0.16; '(most': 0.16; 'bytes': 0.19; 'header:In-Reply-To:1': 0.21; 'last):': 0.23; 'optional': 0.23; 'wrong?': 0.23; 'received:84': 0.25; 'function': 0.25; 'mode': 0.29; 'position.': 0.29; 'import': 0.29; 'unicode': 0.29; 'catching': 0.30; "can't": 0.32; 'file.': 0.32; 'to:addr:python- list': 0.33; 'greatly': 0.33; 'lines': 0.33; 'starting': 0.33; 'error': 0.33; 'file': 0.34; 'there': 0.35; 'header:User-Agent:1': 0.35; '"",': 0.35; 'fails': 0.35; 'reply-to:addr:python.org': 0.35; 'takes': 0.37; 'something': 0.37; 'offset': 0.37; 'sequence': 0.37; 'anything': 0.38; 'but': 0.38; 'execute': 0.38; 'subject:: ': 0.38; 'message:': 0.39; 'doing': 0.39; 'attempt': 0.39; 'to:addr:python.org': 0.39; 'allows': 0.40; 'under': 0.40; 'appreciated.': 0.40; 'help': 0.40; 'results': 0.60; 'within': 0.60; 'back': 0.63; 'perfectly': 0.65; 'subject:? ': 0.67; 'strange': 0.68; 'yourself,': 0.72; 'email addr:hotmail.com': 0.72; 'header:Reply-To:1': 0.72; 'reply-to:no real name:2**0': 0.72; 'subject:line': 0.73; 'presumably': 0.91
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result AiIJAB9f3U1UXebj/2dsb2JhbACXfTGOBXjFXIYcBJRmhCGGMA
Date Wed, 25 May 2011 21:00:18 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10
MIME-Version 1.0
To python-list@python.org
Subject Re: Python 3.2 bug? Reading the last line of a file
References <3d81e2a0-6c86-4f12-a1c4-ce4c736172b6@y31g2000vbp.googlegroups.com>
In-Reply-To <3d81e2a0-6c86-4f12-a1c4-ce4c736172b6@y31g2000vbp.googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To python-list@python.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2091.1306353619.9059.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 82.94.164.166
X-Trace 1306353620 news.xs4all.nl 49039 [::ffff:82.94.164.166]:47496
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:6261

Show key headers only | 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