469,352 Members | 2,150 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Unnatural behaviour in Pickling

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-

Jul 18 '05 #1
1 1267
Pekka Niiranen wrote:
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

--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by gong | last post: by
176 posts views Thread by Thomas Reichelt | last post: by
1 post views Thread by Edward Loper | last post: by
8 posts views Thread by Hans Georg Krauthaeuser | last post: by
1 post views Thread by fedor | last post: by
2 posts views Thread by Kirk Strauser | last post: by
reply views Thread by jkn | last post: by
reply views Thread by Irmen de Jong | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.