[Python-Dev] New lines, carriage returns, and Windows

"Martin v. Löwis" martin at v.loewis.de
Thu Sep 27 07:24:41 CEST 2007

> My understanding is that users can write code that uses only \n and
> Python will write the end-of-line character(s) that are appropriate
> for the platform when writing to a file.  That's what I meant by uses
> \n for everything internally.

Here you misunderstand - that's only the case when the file is opened
in text mode. If the file is opened in binary mode, and you write \n,
then it writes just a single byte (0xA).

> But if you write \r\n to a file Python completely ignores the
> presence of the \r and transforms the \n into a \r\n anyway, hence
> the \r\r in the resulting stream.  My last question is simply does
> anyone find writing \r\r\n when the original string contained \r\n a
> useful behavior - personally I don't see how it is.

That's just for consistency.

> But Guido's response makes this sound like it's a problem w/ VC++
> stdio implementation and not something that Python is explicitly
> doing.

That's correct - it's the notion of "text mode" for file IO.

> Anyway, it'd might be useful to have a text-mode file that
> you can write \r\n to and only get \r\n in the resulting file.

This I don't understand. Why don't you just use binary mode then?
At least for Python 2.x, the *only* difference between text and
binary mode is the treatment of line endings.

For Python 3, things will be different as the distinction goes
further; the precise API for line endings is still being considered


More information about the Python-Dev mailing list