Idiomatic portable way to strip line endings?

John Roth johnroth at
Sun Dec 16 15:54:39 EST 2001

"Martin Bless" <m.bless at> wrote in message
news:3c1cddd3.6188589 at
> [Tim Hammerquist]:
> >Ah. So I was doing my normal over-thinking again.  =)
> Yes - and it's totally appropriate. Danger is not over.
> The line[:-1] idiom works fine on windows, as long you (can) garantee
> that files are opened without the "b" (binary) mode switch.
> If the file is opened 'binary', as in
>   fp = open(fname,'rb')
> no input conversion will be done by the lower level routines. This
> means your line endings are '\r\n' indeed on a windows system.

This is quite true, but if you're opening it in binary mode,
it's questionable whether there's anything that corresponds to
a "line" in the first place. This is, in fact, the standard distinction
between a 'text' and a 'binary' file: text files are organized into
lines, and binary files have some other organization, which is
strictly dependent on the kind of file.

John Roth

More information about the Python-list mailing list