Pickled text file causing ValueError (dos/unix issue)

Tim Peters tim.peters at gmail.com
Fri Jan 14 09:12:49 EST 2005

[Aki Niimura]
> I started to use pickle to store the latest user settings for the tool
> I wrote. It writes out a pickled text file when it terminates and it
> restores the settings when it starts.
> I guess DOS text format is creating this problem.


> My question is "Is there any elegant way to deal with this?".

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).

More information about the Python-list mailing list