the usage of pickle

Oleg Broytmann phd at phd.pp.ru
Fri Aug 10 06:17:55 EDT 2001


On Fri, 10 Aug 2001, Dino Hsu wrote:
> >>> def f():
> 	return '123456'
> >>> f
> <function f at 01215F2C>
>
> >>> pickle.dump(f,'test.txt')

1st error - you cannot dump a function, only a data object. You can
dump/load code objects with marshall module.

> Traceback (most recent call last):
>   File "<pyshell#60>", line 1, in ?
>     pickle.dump(f,'test.txt')
>   File "c:\python21\lib\pickle.py", line 939, in dump
>     Pickler(file, bin).dump(object)
>   File "c:\python21\lib\pickle.py", line 104, in __init__
>     self.write = file.write
> AttributeError: write

2ns error - you've passed text string as 2nd parameter. pickle.dump expects
open file-like object. Examples:

pickle.dump(f, open('test.txt', 'w'))

   or

s = StringIO.StringIO()
pickle.dump(f, s)
# now use s.getvalu() as string dump. Or just use pickle.dumps(f) :)

Oleg.
----
     Oleg Broytmann            http://phd.pp.ru/            phd at phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.





More information about the Python-list mailing list