[Pythonmac-SIG] os.linesep?

Guido van Rossum guido@CNRI.Reston.VA.US
Wed, 03 Nov 1999 07:30:10 -0500


> No. If you're working with text files, just use \n, and make sure to open
> them in text mode. This works correctly on all platforms. The conversion
> between \r and \n is a Mac thing. Similarly, under Windows, Python will
> convert \r\n to \n and vice versa. Under Unix, there is no conversion, and
> therefore there is no difference between text and binary mode.

Let me chime in with one more explanation.  None of this is done by
Python -- it's all behavior defined in the C standard I/O library.  I
think Andres Corrada was the victim of his own attention of detail: he
thought he had to use os.linesep when manipulating text files, while
in fact Python (like C) lets (and requires that!) you use '\n' as the
line separator as long as you open your files in text mode.

One more nit: the Mac is the only platform that *swaps* the native
separator with \n.  On Windows, if you write \r\n, the \r will be
written unchanged, and the stdio library (which has very little memory
:) will translate the \n to \r\n, so your file will contain \r\r\n.
And on Unix, the file will contain exactly what you write.

Advice: don't worry about it, it all -- by magic -- just works!

--Guido van Rossum (home page: http://www.python.org/~guido/)