Reputation: 13
I'm saving a file containing \r with :
with open (filepath, mode = 'w') as file:
file.write(content)
And I read it with :
with open (filepath, mode = 'r') as file:
content = file.read()
I know that the characters \r are present in the saved file, but opening it with Python, they are replaced with \n.
So, how can I open my file with the \r ?
Upvotes: 1
Views: 774
Reputation: 438
Open the file with rb
instead of r
(and wb
instead of w
) . This tells python to open in binary mode, and if you open without it (in text mode) python automatically normalizes end of line chars.
If you want to open in text mode, use the newline = ''
parameter suggested in the other answers.
Upvotes: 2