Why you shouldn't use os.linesep when editing on text mode?

python

Solution

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'

Problem

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?

Original source