Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'example:': 0.03; 'subsequent': 0.04; 'subject:Python': 0.05; 'binary': 0.05; 'python3': 0.05; 'returned.': 0.07; 'subject:file': 0.07; 'python': 0.09; 'buffer,': 0.09; 'bytes,': 0.09; 'methods,': 0.09; 'mode,': 0.09; 'read-ahead': 0.09; 'subject:2.7': 0.09; 'unexpected': 0.09; 'def': 0.10; 'assume': 0.11; '2.7': 0.13; '2.7?': 0.16; '3.3,': 0.16; '314': 0.16; 'code?': 0.16; 'correctly,': 0.16; 'exists?': 0.16; 'iterating': 0.16; 'iterator': 0.16; 'iterators': 0.16; 'oserror:': 0.16; 'subject:between': 0.16; 'wrote:': 0.17; 'documented': 0.17; 'message-----': 0.17; 'skip:i 40': 0.17; 'unicode': 0.17; 'module': 0.19; 'file.': 0.20; 'bit': 0.21; 'import': 0.21; 'assuming': 0.22; 'disabled': 0.22; 'either.': 0.22; 'skip:_ 20': 0.22; 'raise': 0.24; 'idea': 0.24; 'mix': 0.27; 'lines': 0.28; 'behavior.': 0.29; 'behaviour': 0.29; 'mode.': 0.29; 'prints': 0.29; 'received:132': 0.29; 'str': 0.29; 'class': 0.29; 'call.': 0.30; 'mode': 0.30; 'thursday,': 0.30; 'helpful': 0.30; 'code': 0.31; 'file': 0.32; 'skip:- 10': 0.32; 'could': 0.32; 'print': 0.32; 'anywhere': 0.33; 'shorter': 0.33; 'to:addr:python-list': 0.33; 'text': 0.34; 'thanks': 0.34; 'ahead': 0.35; 'so,': 0.35; 'open': 0.35; 'subject:?': 0.35; 'there': 0.35; 'next': 0.35; 'subject:': 0.36; 'but': 0.36; 'email addr:python.org': 0.36; 'anything': 0.36; 'bad': 0.37; 'beyond': 0.37; 'sent:': 0.37; 'quite': 0.37; 'subject:: ': 0.38; 'from:': 0.38; 'files': 0.38; 'fact': 0.38; 'object': 0.38; 'some': 0.38; 'received:10': 0.38; 'gives': 0.39; 'instead': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'telling': 0.61; 'email name:python-list': 0.62; 'between': 0.63; 'details': 0.63; 'behavior': 0.64; 'great': 0.64; 'reply': 0.66; '8bit%:27': 0.71; 'completing': 0.71; '2.7.': 0.84; '2013': 0.84; 'basically,': 0.84; 'me!': 0.84; 'official,': 0.84; 'otten': 0.84; 'peter,': 0.84; 'sometimes.': 0.84; 'subject:over': 0.84 Delivery-date: Thu, 17 Jan 2013 17:38:31 +0100 From: "Wolfgang Maier" To: Subject: RE: iterating over the lines of a file - difference between Python 2.7 and 3? Date: Thu, 17 Jan 2013 17:38:30 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 14.0 Thread-Index: Ac300QJwDsp50jySRbGpd23QIm9x7w== Content-Language: en-us X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 86 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1358440713 news.xs4all.nl 6935 [2001:888:2000:d::a6]:51564 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36952 Thanks Peter, for this very helpful reply and for pointing out _pyio.py to me! It's great to be able to check implementation details sometimes. So, if = I understand you correctly, I can simply import io and open files with io.open() - instead of open and although this is a = bit a detour in Python3 - and this will ensure version-independent = behavior of my code? That=C2=B4s cool! What will my IO object return then when I read from it in Python 2.7? = str where Python3 gives bytes, and unicode instead of str ? This is what = I understood from the Python 2.7 io module doc. -----Original Message----- From: Peter Otten [mailto:__peter__@web.de]=20 Sent: Thursday, January 17, 2013 1:04 PM To: python-list@python.org Subject: Re: iterating over the lines of a file - difference between = Python 2.7 and 3? You can get the Python 3 behaviour with io.open() in Python 2.7. There = is an implementation in Python in _pyio.py: def tell(self): return _BufferedIOMixin.tell(self) - len(self._read_buf) + = self._read_pos Wolfgang Maier wrote: > I just came across an unexpected behavior in Python 3.3, which has to=20 > do with file iterators and their interplay with other methods of=20 > file/IO class methods, like readline() and tell(): Basically, I got=20 > used to the fact that it is a bad idea to mix them because the=20 > iterator would use that hidden read-ahead buffer, so what you got with = > subsequent calls to > readline() or tell() was what was beyond that buffer, but not the next = > thing after what the iterator just returned. >=20 > Example: >=20 > = in_file_object=3Dopen(=E2=80=98some_file=E2=80=99,=E2=80=99rb=E2=80=99) >=20 > for line in in_file_object: >=20 > print (line) >=20 > if in_file_object.tell() > 300: >=20 > # assuming that individual lines are > # shorter >=20 > break >=20 > =20 >=20 > This wouldn=C2=B4t print anything in Python 2.7 since = next(in_file_object)=20 > would read ahead beyond the 300 position immediately, as evidenced by=20 > a subsequent call to in_file_object.tell() (returning 8192 on my = system). >=20 > However, I find that under Python 3.3 this same code works: it prints=20 > some lines from my file and after completing in_file_object.tell()=20 > returns a quite reasonable 314 as the current position in the file. >=20 > I couldn=C2=B4t find this difference anywhere in the documentation. Is = the=20 > 3.3 behavior official, and if so, when was it introduced and how is it = > implemented? I assume the read-ahead buffer still exists? > > By the way, the 3.3 behavior only works in binary mode. In text mode, = the > code will raise an OSError: telling position disabled by next() call. = In > Python 2.7 there was no difference between the binary and text mode > behavior. Could not find this documented either.