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

Peter Hansen peter at engcorp.com
Wed Jun 8 10:15:13 EDT 2005


ajikoe at gmail.com wrote:

> It means in windows we should use 'wb' to write and 'rb' to read ?
> Am I right?

There is a conceptual difference between "text" files and other files 
(which are lumped under the label "binary").

Binary files have any kind of data in them (bytes from 0 to 255) and no 
inherent concept of "lines", and thus no line ending sequences.

Text files generally have no control characters except those used to 
terminate the lines (lines are by definition sequences of bytes followed 
by the line ending sequence).  The line ending sequence is 
platform-dependent: Linux and most other things use just \n (LineFeed), 
while Windows/DOS uses \r\n (CarriageReturn + LineFeed) and the old 
MacOS used just \r (CarriageReturn).

Since the specific line ending used on your platform is rarely important 
to you, so long as you are compatible with other applications that use 
"text" files (such as Notepad), you should use just "r" and "w" to read 
and write files that you consider "text" (and note: it's just a 
convention, in your mind... and at some times a given file might be 
treated the other way, quite legitimately).  Python (actually the 
underlying libraries, I believe) will convert the platform-specific line 
endings to \n when reading text files, and will convert \n to the proper 
line ending sequence for your platform when writing.

If you don't want this conversion, which is unlikely if this is really 
just a text file, then and only then do you want to use "rb" and "wb".

So the answer to the question "What should I be using?" depends entirely 
on you: if you were interested in seeing the raw bytes that Notepad 
wrote, then use "rb".  If you want to work with that file as a *text* 
file, then use just "r".

Note also the existence of the "U" modifier.  Opening a file with "rU" 
will read any of the aforementioned line-ending sequences and convert 
them to just \n, allowing you to work with text files created on other 
platforms.  (I don't believe there's a "wU" and conceptually it's sort 
of meaningless anyway, so you would just use "w" to write the file out 
again.)

-Peter



More information about the Python-list mailing list