are...@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. -- https://mail.python.org/mailman/listinfo/python-list