简体   繁体   中英

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM