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

Peter Hansen peter at engcorp.com
Mon Jun 13 10:52:52 EDT 2005


Steven D'Aprano wrote:
> 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.

This is Python.  Fire up the interactive interpreter and try it out!  It 
will take all of two or three minutes...

> How does this behaviour differ from "universal newlines"?

If you open a text file created with a different line-ending convention 
than that used on your own platform, you may get "interesting" results. 
  If you use "rU" instead, you will receive only \n line endings and not 
have anything to worry about.  (For example, reading a Windows text file 
on Linux will give you lines that have \r\n endings in them... not what 
you really want.  Using "rU" will give you just \n line endings whether 
you are on Linux or Windows.)

-Peter



More information about the Python-list mailing list