Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'pickle' an object to a certain directory?

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?

like image 654
Houdini Avatar asked Jul 19 '13 15:07

Houdini


People also ask

Can you pickle any object?

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.

How do you pickle an object in Python?

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.)

What function do you call to pickle an object?

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.

Can pickle save objects?

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.


1 Answers

with open('/full/path/to/file', 'wb') as f:
    pickle.dump(object, f)
like image 77
user2357112 supports Monica Avatar answered Oct 17 '22 07:10

user2357112 supports Monica