0

I have following code:

import os
with open("save.txt", "a") as file:
    file.write("abc" + "\n")
    file.write("abc" + "\r\n")
    file.write("abc" + os.linesep)
    file.write("abc" + "\r")

As far as I understood, on Linux and Mac with "\n" a line break is done, while on Windows systems "\r\n" is required. It actually even came up in THIS stack overflow post. However, when I check the result of my code, \n is a simple line break, while \r\n is a double line break (and so is os.linesep, probably as it's translated to \r\n). And out of curiosity checking for \r shows that this is also a simple line break. So I assume that \r\n are simply two line breaks.

My question is now: Why? Did Windows change in recent years or Python? I use Windows 10 and Python 3.10, has for any of those two changed? Or is there simply a misunderstanding from my side what it refers to in regard of \r\n in the linked post?

Thanks for clarification.

PS: To bring even more madness into this... The txt file shows simple line break for \n and \r in regular Windows Notepad, in Notepad++ and internally in PyCharm, while \r\n and os.linesep are double line breaks. However, when opening with WordPad, then \n and \r are yet again simple breaks while \r\n and os.linesep are not shown, causing "abcabc".

Zunderding
  • 61
  • 7
  • `\n` in your Python code is replaced by a platform-appropriate line ending when you write your file. – khelwood Nov 14 '21 at 20:29

0 Answers0