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

SourceForge.net noreply at sourceforge.net
Mon May 7 23:15:40 CEST 2007


Bugs item #1714381, was opened at 2007-05-07 11:50
Message generated for change (Comment added) made by gagenellina
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: Open
Resolution: None
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: Gabriel Genellina (gagenellina)
Date: 2007-05-07 18: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 17: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 14: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