\r\n or \n notepad editor end line ???

Steven D'Aprano steve at REMOVETHIScyber.com.au
Mon Jun 13 16:34:53 CEST 2005


On Mon, 13 Jun 2005 11:53:25 +0200, Fredrik Lundh wrote:

> <ajikoe at gmail.com> wrote:
> 
>> It means in windows we should use 'wb' to write and 'rb' to read ?
>> Am I right?
> 
> no.
> 
> you should use "wb" to write *binary* files, and "rb" to read *binary*
> files.
> 
> if you're working with *text* files (that is, files that contain lines of text
> separated by line separators), you should use "w" and "r" instead, and
> treat a single "\n" as the line separator.

I get nervous when I read instructions like this. It sounds too much like
voodoo: "Do this, because it works, never mind how or under what
circumstances, just obey or the Things From The Dungeon Dimensions will
suck out your brain!!!"

Sorry Fredrik :-)

When you read a Windows text file using "r" mode, what happens to the \r
immediately before the newline? Do you have to handle it yourself? Or will
Python cleverly suppress it so you don't have to worry about it?

And when you write a text file under Python using "w" mode, will the
people who come along afterwards to edit the file in Notepad curse your
name? Notepad expects \r\n EOL characters, and gets cranky if the \r is
missing.

How does this behaviour differ from "universal newlines"?



-- 
Steven





More information about the Python-list mailing list