[Tutor] cPickle.load()

Terry Carroll carroll at tjc.com
Sat Jul 16 19:31:44 CEST 2005

On Sat, 16 Jul 2005, David Jimenez wrote:

> [EOFError on pickle]

First, something that may not be a problem, but let's get it out of the 
way, anyway:

> pickle_file=open("pickles1.dat","w")
>    . . .
> pickle_file=open("pickles1.dat","rb")

I notice that your write open does not specify binary mode, but your read 
open does.  If you're on an OS where binary mode matters (e.g., Windows), 
you should use it in both places, or the file you write out will probably 
be corrupted.

But here's your real problem (I'm adding line numbers in brackets for 

> for i in pickle_file:            #1
>     i=cPickle.load(pickle_file)  #2
>     print i                      #3
> pickle_file.close()              #4

On line #1, you're telling it to iterate over the pickel file.  That is, 
line #1 says, read a line from pickle_file, and assign it to i.

Then, in line #2, bearing in mind you've already taken the first line out 
of the file, you're doing a pickle load, starting part-way into the file.

I suspect that, because it's a binary file, your line #1 is putting the 
whole file contents into i, and nothing's left in the file to process.  
When you do the load, you hit EOF, hence the error.

It should be a big red flag to you that you're modifying variable i both 
in line #1 and line #2.  That never leads anywhere good.

What I would suggest is that you endlessly loop over a load call, and wrap 
it in a try/except combination to exit on EOF.  Here's an equivalent to 
the above four lines, that takes that approach:

    while True:
        print i
except EOFError:

Hope this helps.

More information about the Tutor mailing list