How to write Unix end of line characters in Windows?

后端 未结 3 1006
执笔经年
执笔经年 2020-11-27 14:42

How can I write to files using Python (on Windows) and use the Unix end of line character?

e.g. When doing:

f = open(\'file.txt\', \'w\')
f.write(\'hello\\n         


        
相关标签:
3条回答
  • 2020-11-27 14:59

    The modern way: use newline=''

    Use the newline= keyword parameter to io.open() to use Unix-style LF end-of-line terminators:

    import io
    f = io.open('file.txt', 'w', newline='\n')
    

    This works in Python 2.6+. In Python 3 you could also use the builtin open() function's newline= parameter instead of io.open().

    The old way: binary mode

    The old way to prevent newline conversion, which does not work in Python 3, is to open the file in binary mode to prevent the translation of end-of-line characters:

    f = open('file.txt', 'wb')    # note the 'b' meaning binary
    

    but in Python 3, binary mode will read bytes and not characters so it won't do what you want. You'll probably get exceptions when you try to do string I/O on the stream. (e.g. "TypeError: 'str' does not support the buffer interface").

    0 讨论(0)
  • 2020-11-27 15:02

    For Python 2 & 3

    See: The modern way: use newline='' answer on this very page.

    For Python 2 only (original answer)

    Open the file as binary to prevent the translation of end-of-line characters:

    f = open('file.txt', 'wb')
    

    Quoting the Python manual:

    On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files. On Unix, it doesn’t hurt to append a 'b' to the mode, so you can use it platform-independently for all binary files.

    0 讨论(0)
  • 2020-11-27 15:05

    You'll need to use the binary pseudo-mode when opening the file.

    f = open('file.txt', 'wb')
    
    0 讨论(0)
提交回复
热议问题