[ python-Bugs-1714381 ] Universal line ending mode duplicates all line endings

SourceForge.net noreply at sourceforge.net
Fri May 11 20:50:56 CEST 2007


Bugs item #1714381, was opened at 2007-05-07 16:50
Message generated for change (Comment added) made by gjb1002
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1714381&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Windows
Group: Python 2.5
Status: Closed
Resolution: Wont Fix
Priority: 5
Private: No
Submitted By: Geoffrey Bache (gjb1002)
Assigned to: Nobody/Anonymous (nobody)
Summary: Universal line ending mode duplicates all line endings

Initial Comment:
On Windows XP, reading a file produced by Windows XP with universal line endings produces twice as many lines!

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> open("winlineend").read()
'Hello\r\n'
>>> open("winlineend", "rU").read()
'Hello\n\n'


I would expect the last to give "Hello\n". 

----------------------------------------------------------------------

>Comment By: Geoffrey Bache (gjb1002)
Date: 2007-05-11 20:50

Message:
Logged In: YES 
user_id=769182
Originator: YES

Which doc has been changed? Can I review it somewhere? 

----------------------------------------------------------------------

Comment By: Georg Brandl (gbrandl)
Date: 2007-05-11 20:41

Message:
Logged In: YES 
user_id=849994
Originator: NO

Okay, that settles it.

When you write "\r\n" to a Windows text file, it writes "\r\r\n"
(since "\n" is converted to "\r\n"). When universal newline mode
sees that, it thinks of it as Mac-lineend followed by a Windows-
lineend.

The doc fix has been committed, closing this one as "won't fix".

----------------------------------------------------------------------

Comment By: Geoffrey Bache (gjb1002)
Date: 2007-05-11 20:34

Message:
Logged In: YES 
user_id=769182
Originator: YES

It returns "Hello\r\r\n" (not "Hello\r\n\n" as was suggested earlier)
Don't know quite how it does this from simply writing os.linesep.


----------------------------------------------------------------------

Comment By: Georg Brandl (gbrandl)
Date: 2007-05-11 13:06

Message:
Logged In: YES 
user_id=849994
Originator: NO

To the OP: what does "open("winlineend", "rb").read()" return?

----------------------------------------------------------------------

Comment By: Gabriel Genellina (gagenellina)
Date: 2007-05-08 03:48

Message:
Logged In: YES 
user_id=479790
Originator: NO

Yes, it's not so obvious, and the same question was posted a few days ago
on comp.lang.python.
I'll review the documentation and make it more clearly stated.


----------------------------------------------------------------------

Comment By: Geoffrey Bache (gjb1002)
Date: 2007-05-07 23:36

Message:
Logged In: YES 
user_id=769182
Originator: YES

I see. This seems like something of a gotcha to me: is it documented
anywhere? Seems to make os.linesep not very useful in fact, especially if
"\n" will work fine everywhere and is shorter. 

It isn't at all obvious that file.write(os.linesep) will in fact write two
lines...


----------------------------------------------------------------------

Comment By: Gabriel Genellina (gagenellina)
Date: 2007-05-07 23:15

Message:
Logged In: YES 
user_id=479790
Originator: NO

> file.write("Hello" + os.linesep)

The line separator for a file opened in *text*mode* is *always* \n in
Python code. It gets converted to os.linesep when you write the file; and
conversely, os.linesep get translated into a single \n when you read the
file.
On Windows, os.linesep is "\r\n". The argument to file.write above is 
"Hello\r\n". That "\n" gets translated into "\r\n" when it is written, so
the actual file contents will be "Hello\r\n\n"

In short: if you open a file in text mode (the default) *don't* use
os.linesep to terminate lines. Only use os.linesep when writing text files
open in binary mode (and that's not a common case).


----------------------------------------------------------------------

Comment By: Geoffrey Bache (gjb1002)
Date: 2007-05-07 22:27

Message:
Logged In: YES 
user_id=769182
Originator: YES

I create the file as follows: 

>>> import os
>>> file = open("test.txt", "w")
>>> file.write("Hello" + os.linesep)
>>> file.close()
>>> open("test.txt").read()
'Hello\r\n'
>>> open("test.txt", "rU").read()
'Hello\n\n'


----------------------------------------------------------------------

Comment By: Raghuram Devarakonda (draghuram)
Date: 2007-05-07 19:39

Message:
Logged In: YES 
user_id=984087
Originator: NO


I created a file "test.txt" with notepad whose contents are "hello\r\n".
Both open().read() and open("rU").read() returned 'hello\n'. I tested with
both 2.5 and 2.5.1 (installed using installers from python.org) and the
result is same on both. Can you elaborate your test case more? How is this
file "winlineend" created?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1714381&group_id=5470


More information about the Python-bugs-list mailing list