The behavior you are seeing is not python-specific. It comes from the buffered file-handling functions in the C standard library that underlies python and other high level languages. Unless told not to, it will convert newline characters to the current platform's native text file line break sequence when writing, and do the reverse when reading. See the documentation for fopen()
on your local system for details. On Windows, it means \n will be converted to \r\n on writes.
The python docs mention newline conversion and other open()
mode options here.
One solution would be to use open("filename", "wb")
instead of open("filename", "w")
when opening the output file in the first place. That will avoid the automatic newline conversion. It ought to solve the problem for your boss, so long as your boss is using some form of unix (including OSX). Unfortunately, it will also mean that some Windows text editors (e.g. notepad?) will present your file strangely:
Windows acts like a teletype
when it sees new lines
without carriage returns.
Another approach would be to convert your files as needed before sending them to someone who doesn't use Windows. Various conversion programs exist for this purpose, such as dos2unix
and flip
.