[Tutor] EOF error with pickle?

Terry Carroll carroll at tjc.com
Wed Dec 24 12:15:55 EST 2003


On Wed, 24 Dec 2003, Clay Shirky wrote:

> Here's the test script I wrote to reproduce the error:
> 
> import pickle
> test_list = range(100)
> 
> f = open("pickle.dat", 'w')
> pickle.dump(test_list, f)
> print "dumped..."           # this works

f.close()

> g = open("pickle.dat")
> new_list = pickle.load(g)
> print new_list              # this generates the error below

I recognize this error; you must have copied it from me!

As an aside, if you decide to use binary pickling (slightly more 
efficient):

  pickle.dump(test_list, f, bin=1)

and you're on Windows, you'll want to make sure your opens are binary:

  f = open("pickle.dat", 'wb')
   ...
  g = open("pickle.dat", 'rb')

-- 
Terry Carroll
Santa Clara, CA
carroll at tjc.com 




More information about the Tutor mailing list