\r\n or \n notepad editor end line ???
Peter Hansen
peter at engcorp.com
Mon Jun 13 20:26:48 EDT 2005
Steven D'Aprano wrote:
> So going back to the original question... if I open in "r" mode a text
> file which was created under Windows, I will get \r characters in the
> text and have to deal with them regardless of what platform I am running
> Python under. Correct?
Almost, but the way you phrased that makes it incorrect in at least one
case. I suspect you are clear on the actual situation now, but just to
correct your wording: you will get \r characters and have to deal with
them on any platform which does *not* use \r\n for its line endings.
(Under Windows, of course, you won't have to deal with them at all.)
> If I use "rU" mode Python suppress the \r characters. When I write a
> string back, I'm responsible for making sure the EOL markers are correct
> for whatever platform I expect the file to be read under. Unless Python
> can somehow magically read my mind and know that even though I'm writing
> the file under Linux it will be read later under Windows. Am I close?
Python can't magically read your mind, although if you manage to steal
the time machine (in the future) you could always just come back and fix
the files as they are written, and you (in present time) would *think*
that Python had magically read your mind...
A few more trinkets: some programs on Windows will work just fine even
if they encounter files with only \n line endings. Decent text editors
will either adapt or let you specify what line ending should be
expected. Trying always to work with only \n line endings in files you
write will help us move towards the day when we can obliterate the
abomination that is \r\n so try it out and use "rU" to read, "w" to
write on Linux if that works in your case.
-Peter
More information about the Python-list
mailing list