Normally, executing the following code will pickle an object to a file in my current
directory:
fp = open('somefile.txt', 'wb')
pickle.dump(object, fp)
How do I re-direct the output from pickle.dump
to a different directory?
What can you Pickle? Generally you can pickle any object if you can pickle every attribute of that object. Classes, functions, and methods cannot be pickled -- if you pickle an object, the object's class is not pickled, just a string that identifies what class it belongs to.
Python pickle module is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled so that it can be saved on disk. What pickle does is that it “serializes” the object first before writing it to file. Pickling is a way to convert a python object (list, dict, etc.)
Pickling (and unpickling) is alternatively known as “serialization”, “marshalling,” 1 or “flattening”; however, to avoid confusion, the terms used here are “pickling” and “unpickling”. The pickle module is not secure. Only unpickle data you trust.
Pickling is a method to convert an object (list, dict, etc) to a file and vice versa. The idea is to save one or more objects in one script and load them in another. You can also use it to save program or game states.
with open('/full/path/to/file', 'wb') as f:
pickle.dump(object, f)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With