Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!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; '"""': 0.07; '(python': 0.07; 'attribute': 0.07; 'bug.': 0.09; 'method,': 0.09; 'newline': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:set': 0.09; 'python': 0.11; 'bug': 0.12; 'newlines': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:when': 0.16; 'tried:': 0.16; 'tuple': 0.16; 'unix- style': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'skip:f 30': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'gets': 0.27; 'header:X -Complaints-To:1': 0.27; 'mode': 0.30; '>>>>': 0.31; 'apparently': 0.31; 'universal': 0.31; 'file': 0.32; 'text': 0.33; 'open': 0.33; 'url:python': 0.33; 'objects': 0.35; 'but': 0.35; 'subject:?': 0.36; 'url:org': 0.36; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'expect': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'according': 0.40; 'first': 0.61; 'email addr:gmail.com': 0.63; 'believe': 0.68; 'containing': 0.69; 'subject:get': 0.81; 'hand,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: when does newlines get set in universal newlines mode? Date: Mon, 04 May 2015 14:01:05 +0200 Organization: None References: <3c45772b-77e0-4c17-8b3d-aa246c4b511c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd9422.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430740879 news.xs4all.nl 2852 [2001:888:2000:d::a6]:55274 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89897 arekfu@gmail.com wrote: > Hi all, > > I have a text file with Windows-style line terminators (\r\n) which I open > in universal newlines mode (Python 2.7). I would expect the newlines > attribute to be set after the first call to the readline() method, but > apparently this is not the case: > >>>> f=open('test_crlf', 'rU') >>>> f.newlines >>>> f.readline() > 'foo\n' >>>> f.newlines >>>> f.readline() > 'bar\n' >>>> f.newlines > '\r\n' > On the other hand, the newlines attribute gets set after the first call to > readline() on a file with Unix-style line endings. > > Is this a bug or a feature? According to https://docs.python.org/2.7/library/functions.html#open """ If Python is built without universal newlines support a mode with 'U' is the same as normal text mode. Note that file objects so opened also have an attribute called newlines which has a value of None (if no newlines have yet been seen), '\n', '\r', '\r\n', or a tuple containing all the newline types seen. """ I tried: >>> with open("tmp.txt", "wb") as f: f.write("alpha\r\nbeta\rgamma\n") ... >>> f = open("tmp.txt", "rU") >>> f.newlines >>> f.readline() 'alpha\n' >>> f.newlines # expected: '\r\n' >>> f.readline() 'beta\n' >>> f.newlines '\r\n' # expected: ('\r', '\r\n') >>> f.readline() 'gamma\n' >>> f.newlines ('\r', '\n', '\r\n') I believe this is a bug.