Pickled text file causing ValueError (dos/unix issue)
sjmachin at lexicon.net
Fri Jan 14 21:32:48 CET 2005
On Fri, 14 Jan 2005 09:12:49 -0500, Tim Peters <tim.peters at gmail.com>
>> 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).
Tim, the manual as of version 2.4 does _not_ mention the need to use
'b' on OSes where it makes a difference, not even in the examples at
the end of the chapter. Further, it still refers to protocol 0 as
'text' in several places. There is also a reference to protocol 0
files being viewable in a text editor.
In other words, enough to lead even the most careful Reader of TFM up
the garden path :-)
More information about the Python-list