On linux, the text file written by python uses LF (\n
) as EOL by default. While on windows, the default EOL is CRLF(\r\n
).
with open(file_path, 'w', newline='\n') as f:
f.write(text)
The key point is newline='\n'
, it specifies the file EOL.
By default, open
uses os.linesep
as file EOL. On linux, os.linesep
is \n
. On windows, os.linesep
is \r\n
.