Unnatural behaviour in Pickling

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Thu Jun 17 14:44:21 CEST 2004


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



More information about the Python-list mailing list