line break confusion

Quinn Dunkan quinn at seniti.ugcs.caltech.edu
Sat Feb 16 05:20:07 EST 2002


On Fri, 15 Feb 2002 21:01:18 -0800, Michael Mell <mike at nthwave.net> wrote:
>Sean 'Shaleh' Perry wrote:
>
>> Windows: \r\n
>> Linux (UNIX): \n
>> Mac: \r
>>
>> Each OS's libs transform the sequence '\n' to whatever the right thing is for
>> them.
>
>Thanks for answering such an apparently ignorant question, but the answer is not
>so simple.
>
>I want to read and write files with line breaks which are not of the native
>platform type.
>
>Problem #1 is that on a Mac, the values of Mac and Unix are /swapped/ and
>therefore wrong. Writing a file using '\n' results in a /Mac/ line break (as
>reported by BBEdit, the venerable Mac editor). Writing a file using '\r'

That's what he said above.  The Mac stdio converts \n to the right thing (\r on
a Mac).


>reported by BBEdit, the venerable Mac editor). Writing a file using '\r'
results
>in a /Unix/ file. The Mac seems unable to write Windows \r\n no matter what
>order is used.

But that seems weird.

>Problem #2 is that when reading a Windows file on Linux or Mac, a /single/ line
>('\r\n') is interpreted as /two/ lines ('\n\n'). Files written out on Linux
then >have double the correct number of lines.

That's also weird.  Doesn't happen for me when I read CRLFs on unixes.


You should probably open your files in binary mode, to get stdio to leave the
line ending alone.  Then do your own EOL translation.



More information about the Python-list mailing list