Python 2.7 documentation (and Python 3 documentation as well) contain the following line about the os.linepath
function:
Do not use os.linesep as a line terminator when writing files opened in text mode (the default);
Why is that? And how is it different from using it on binary mode?
When you open a file in text mode any \n
that you write to the file is converted to the appropriate line ending for the platform you are using.
So, for example if you were on Windows where os.linesep
is '\r\n'
, when you write that to a file the \n
will get automatically converted to \r\n
and you will end up with \r\r\n
written to your file.
For example:
>>> import os
>>> os.linesep
'\r\n'
>>> with open('test.txt', 'w') as f:
... f.write(os.linesep)
...
>>> with open('test.txt', 'rb') as f:
... print repr(f.read())
...
'\r\r\n'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With