What does this error mean?

I’m trying to do something where I save a pickle file to a specific folder. In the pickle.dump(data, file) line, I have data as what to save and file as the folder to save it to, but it says I need a ‘write’ tag on the file variable. What do I change? The variable is equal to another variable, and looks like this: file = pkl_file
When I reach the part of the program that tries to save the pickle file, I get this error message:

Traceback (most recent call last): File "/Users/tellinghawk4837/PythonOS-main/Python OS Beta 1.1.py", line 26, in <module> pickle.dump(data,file) TypeError: file must have a 'write' attribute

What do I change to make it work?

It looks like you are passing a string filename to pickle.dumps, but it is expecting a file object.

You can fine more information in the documentation: pickle — Python object serialization — Python 3.9.2 documentation

The solution would look something like:

with open(pkl_file, "wb") as fp:
    pickle.dump(data, fp)
1 Like

Thanks for your help!

Uh… I don’t think I did this right.

This is my code, I don’t think I’ve done this part right.