Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python pickle gives "AttributeError: 'str' object has no attribute 'write'"

When I try to pickle something, I get an AttributeError: 'str' object has no attribute 'write'

An example:

import pickle
pickle.dump({"a dict":True},"a-file.pickle")

produces:

...
AttributeError: 'str' object has no attribute 'write'

What's wrong?

like image 366
drevicko Avatar asked Sep 21 '14 11:09

drevicko


1 Answers

It's a trivial mistake: pickle.dump(obj,file) takes a file object, not a file name.

What I need is something like:

with open("a-file.pickle",'wb') as f:
    pickle.dump({"a dict":True},f)
like image 198
drevicko Avatar answered Oct 12 '22 13:10

drevicko