Modern way: use newline = ''
Use the newline= keyword parameter for io.open () to use Unix-style end-line end-terminals:
import io f = io.open('file.txt', 'w', newline='\n')
This works in Python 2.6+. In Python 3, you can also use the built-in parameter open() function newline= instead of io.open() .
Old way: binary mode
The old way to prevent newline conversion that doesn't work in Python 3 is to open the file in binary mode to prevent trailing characters:
f = open('file.txt', 'wb') # note the 'b' meaning binary
but in Python 3, binary mode will read bytes, not characters, so it wonβt do what you want. You will probably get exceptions when you try to perform string I / O on a thread. (for example, "TypeError:" str "does not support the buffer interface").
Colin D Bennett May 2 '14 at 18:25 2014-05-02 18:25
source share