[Tutor] Cannot cPickle.load()

Hugo González Monteverde hugonz-lists at h-lab.net
Wed Dec 1 00:16:17 CET 2004

Hi Tutors,

I think I must be missing something here. I pickled a dictionary of 
lists into a file, using protocol 2, some weeks ago.  Now I'm trying to 
load it and I can't.

File is 21kB long, so I know it has data in it, and when I read it into 
a string (without pickling) I understand the pickle is there. Here's  my 

IDLE 1.0.3
 >>> import cPickle
 >>> import pickle
 >>> myfilep=open("e:\Devel\listpickled", "r")
 >>> myfilep
<open file 'e:\Devel\listpickled', mode 'r' at 0x00AA05A0>
 >>> mydict = cPickle.load(myfilep)

Traceback (most recent call last):
   File "<pyshell#4>", line 1, in -toplevel-
     mydict = cPickle.load(myfilep)
 >>> myfilep.tell()
 >>> myfilep.seek(0)
 >>> mydict= pickle.load(myfilep)

Traceback (most recent call last):
   File "<pyshell#7>", line 1, in -toplevel-
     mydict= pickle.load(myfilep)
   File "C:\Python23\lib\pickle.py", line 1390, in load
     return Unpickler(file).load()
   File "C:\Python23\lib\pickle.py", line 872, in load
   File "C:\Python23\lib\pickle.py", line 1189, in load_binput
     i = ord(self.read(1))
TypeError: ord() expected a character, but string of length 0 found
 >>> myfilep.tell()
 >>> myfilep.seek(0)
 >>> mystring = myfilep.read()
 >>> mystring
 >>> #But this is not the full contents of the file!
 >>> myfilep.tell()
 >>> len(mystring)
 >>> mystring = myfilep.read()
 >>> len(mystring)
 >>> mystring = myfilep.read()
 >>> len(mystring)
 >>> #ok, done with the reading

I cannot make any sense of it. Both implementations of load() tell me 
they're encountering EOF right away. If I use read, there's the pickled 
data, but I cannot read the entire file into the variable.

Please help if possible. I don't know what elese to do... (!)


More information about the Tutor mailing list