Unnatural behaviour in Pickling

P

Pekka Niiranen

Hi there,

why must file be opened in "r" -mode, before loading works
with Pickling? This forces me to close the file between pickle.dump
and pickle.load. :(

Example:
-----************------

a = {'pekka': 1, 'jussi': 2}
fh = open('c:\temp\log.log', "wb")
pickle.dump(a,fh)
fh.close()

**** now:
fh = open('c:\temp\log.log', "wb")
b = pickle.load(fh)
**** gives error:
IOError: (0, 'Error')

**** however:
fh = open('c:\temp\loglog', "rb")
b = pickle.load(fh)
**** works.

-pekka-
 
R

Reinhold Birkenfeld

Pekka said:
Hi there,

why must file be opened in "r" -mode, before loading works
with Pickling? This forces me to close the file between pickle.dump
and pickle.load. :(

Example:
-----************------

a = {'pekka': 1, 'jussi': 2}
fh = open('c:\temp\log.log', "wb")
pickle.dump(a,fh)
fh.close()

**** now:
fh = open('c:\temp\log.log', "wb")
b = pickle.load(fh)
**** gives error:
IOError: (0, 'Error')

**** however:
fh = open('c:\temp\loglog', "rb")
b = pickle.load(fh)
**** works.

Well, unpickling involves reading from the file, so you must open it in
read and write mode, e.g. "w+b".
BTW, you should reset the file pointer between dumping and loading by
using seek().

Reinhold
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top