File Object behavior

7stud bbxx789_05ss at yahoo.com
Tue Apr 3 14:33:58 EDT 2007


On Apr 3, 12:26 pm, "7stud" <bbxx789_0... at yahoo.com> wrote:
> The file.writelines() documentation says that it
> doesn't add line separators. Is adding a carriage return something
> different?

No.

> Is this expected behavior?

According to Python in a Nutshell(p. 217), it is.  On windows, in
text
mode, when you write a \n to a file, the \n is converted to the
system
specific newline (which is specified in os.linesep).  For windows, a
newline is \r\n.  Conversely, on windows, in text mode, when you read
a \r\n newline from a file, it is converted to a \n.

I forgot to add that when you read or write in binary mode, no
conversion takes place.  So, if you read \r\n from the file, your
input will contain the \r\n; and if you write \r\n to the file, then
the file will contain \r\n.




More information about the Python-list mailing list