Why 'r' mode anyway? (was: Re: Pickled text file causing ValueError (dos/unix issue))

Serge Orlov Serge.Orlov at gmail.com
Fri Jan 14 14:46:57 EST 2005


Irmen de Jong wrote:
> Tim Peters wrote:
>
> > Yes:  regardless of platform, always open files used for pickles in
> > binary mode.  That is, pass "rb" to open() when reading a pickle
file,
> > and "wb" to open() when writing a pickle file.  Then your pickle
files
> > will work unchanged on all platforms.  The same is true of files
> > containing binary data of any kind (and despite that pickle
protocol 0
> > was called "text mode" for years, it's still binary data).
>
> I've been wondering why there even is the choice between binary mode
> and text mode. Why can't we just do away with the 'text mode' ?

We can't because characters and bytes are not the same things. But I
believe what you're really complaining about is that "t" mode sometimes
mysteriously corrupts data if processed by the code that expects binary
files. In Python 3.0 it will be fixed because file.read will have to
return different objects: bytes for "b" mode, str for "t" mode. It
would be great if file type was split into binfile and textfile,
removing need for cryptic "b" and "t" modes but I'm afraid that's too
much of a change even for Python 3.0

  Serge.




More information about the Python-list mailing list