Idiomatic portable way to strip line endings?

Chris Perkins chris.perkins at inbusiness.com
Sun Dec 16 16:23:31 EST 2001


Tim Hammerquist <tim at vegeta.ath.cx> wrote in message news:<slrna1pbik.6jf.tim at vegeta.ath.cx>...

> In general, I can use:
> 
>     line = line[:-1]
> or
>     del line[-1:]
> 
> to strip the last character off the line, but this only works on
> operating systems that have a one-byte line separator like Unix/Linux
> ('\n').  The Win32 line separator is 2-bytes ('\r\n'), so this
> solution is not portable.

Actually, line[:-1] works just fine on Windows, as long as you read
the file in non-binary mode, because the '\r\n' line-endings are all
turned into '\n' when the file is read. They're also turned back into
'\r\n' when you write the file, which has the interesting effect that
you  can turn Unix text-files into Windows text-files like this:

t = open(filename,'r').read()
open(filename,'w').write(t)

This is handy, but also somewhat disturbing - the file is changed by
writing its contents back to it seemingly unaltered.

>     line.replace(os.linesep, '')
> 
> The above works when iterating over text files, but fails if only a
> final linesep should be stripped. OTOH, I've seen the non-portable:
> 
>     line = line[:-1]
> 
> in production code.

I've found that os.linesep causes more trouble than it solves when
trying to write portable, file-writing code. Seemingly innocent code
like this:

f = open(filename,'r')
f.write(os.linesep.join(['One','Two','Three']))
f.close()

actually writes a file with screwed-up line-endings on Windows -
'\r\r\n' to be precise (which, I have discovered, shows up
single-spaced in some Windows text-editors and double-spaced in
others).
f.write('\n'.join(['One','Two','Three'])) ,on the other hand, does the
right thing on both Windows and Linux.
So unless I'm using binary mode for reading and writing files, I use
'\n' and line[:-1] everywhere. It's odd that hard-coding '\n' is more
portable than using os.linesep, but I've just gotten used to it.

Chris Perkins



More information about the Python-list mailing list