Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Writing to a new file - But wrong EOL Conversion

Tags:

python

file

My script writes to and creates a new file but it is currently making it in Mac EOL Conversion instead of Windows. This means that each line ends with only 'CR' instead of 'CR LF' which won't work for what i'm trying to do.

Now why this is, or how I can change it?

f = open('...')
text_file1.write(str(i) + ',' + harvestServer + ',' + finalString + harvestCommand + '\r')
text_file1.close()
like image 701
nickp90 Avatar asked Nov 20 '25 22:11

nickp90


1 Answers

Replace the \r with \n, having made sure you open the file in text mode. This will use the native convention for your platform (that is, os.linesep).

Alternatively, open the file in binary mode and use \r\n. This will use the Windows convention no matter where you run your code.

Finally, you can control the newline translation by giving the optional newline argument to open().

like image 69
NPE Avatar answered Nov 22 '25 12:11

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!