[Pythonmac-SIG] Re: MacPython and line-endings

Oliver Steele steele@alphamask.com
Thu, 11 Oct 2001 09:24:09 -0400


When I was doing a lot of cross-platform Python development a few years ago,
I solved the line ending problem with a file-like wrapper class that
normalized line endings into '\n', regardless of the platform you were
running on and of the line ending convention that the file used.  For
example

>>> from textopen import textopen
>>> file1 = textopen(pathname)
is equivalent to
>>> file2 = open(pathname)

except that file1.readline() will return the first line that's terminated by
any
of '\n' (UNIX), '\r' (MacOS), or '\r\n' (PC), whereas file2.readline() will
look for a different line separator depending on the execution platform (and
won't generally work for files that use a different convention).

The file is at http://www.cs.brandeis.edu/~steele/sources/textopen.py.  I
haven't used it in several years, but it worked then :-).

Oliver Steele
steele@osteele.com