Using python to write text files with DOS line endings on linux

后端 未结 5 1783
野性不改
野性不改 2021-02-18 17:40

I want to write text files with DOS/Windows line endings \'\\r\\n\' using python running on Linux. It seems to me that there must be a better way than manually putting a \'\\r\\

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-18 18:05

    Just write a file-like that wraps another file-like and which converts \n to \r\n on write.

    For example:

    class ForcedCrLfFile(file):
        def write(self, s):
            super(ForcedCrLfFile, self).write(s.replace(r'\n', '\r\n'))
    

提交回复
热议问题