Python 2.7 documentation (and Python 3 documentation as well) contain the following line about the os.linepath
function:
Do not use os.li
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'