Search code examples
pythonwindowspython-3.xnewlinemultiline

Why is multiline string altered on print or write? (Python 3.6 on Windows)


I am using Python 3.6.3 on Windows. When I try to print a single multiline string to file like this:

with open('test1.txt', mode='w') as f:
    f.write('test\r\ntest\r\n')

Then test1.txt will end up containing test\r\r\ntest\r\r\n instead of test\r\ntest\r\n.

A workaround to get the desired output would look like so:

with open('test2.txt', mode='w') as f:
    for line in 'test\r\ntest\r\n'.splitlines():
        print(line, file=f)

Why is this happening?


Solution

  • Well, turns out properly stating a question often leads to the answer presenting itself:

    The reason for this behaviour is to be found with Python's universal newline (quoting from PEP 3116 on TextIOWrapper):

    On output, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string. (Note that the rules guiding translation are different for output than for input.)

    This means, when opening the file for writing, the newline parameter can be set to '' to achieve the desired output:

    with open('test3.txt', mode='w', newline='') as f:
        f.write('test\r\ntest\r\n')