Trying to write a cPickle object but get a ‘write’ attribute type error

The second argument to cPickle.dump() must be a file object. You passed in a string containing a filename instead.

You need to use the open() function to open a file object for that filename, then pass the file object to cPickle:

with open(outfile, 'wb') as pickle_file:
    cPickle.dump(all, pickle_file)

See the Reading and Writing Files section of the Python tutorial, including why using with when opening a file is a good idea (it’ll be closed for you automatically).

Leave a Comment