Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: saving objects and using pickle. extension of filename

Tags:

Hello I´m trying using the next piece of code:

import pickle  object = Object()  filehandler = open(filename, 'w')  pickle.dump(object, filehandler)  

I would like to know what should be the extension of the file 'filename'. Thank you!

like image 701
Peterstone Avatar asked Dec 25 '10 11:12

Peterstone


People also ask

What is the extension of pickle file Python?

File created by pickle, a Python module that allows objects to be serialized to files on disk and deserialized back into the program at runtime; saves a byte stream that represents the objects; more often uses the . P extension rather than ". pickle."

Can you pickle objects 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.)

Can pickle store class objects in Python?

The pickle module can store things such as data types such as booleans, strings, and byte arrays, lists, dictionaries, functions, and more.


2 Answers

You could use any filename, but as an FYI it's common to use ".p" (for obvious reasons).

pickle.dump( favorite_color, open( "save.p", "wb" ) ) 

Read: UsingPickle

like image 97
Krishan Gupta Avatar answered Oct 15 '22 08:10

Krishan Gupta


One more thing you should pay attention to:

you should used binary mode with operation of pickling files. So 'w' should be 'wb'.

like image 44
Ethan Dong Avatar answered Oct 15 '22 09:10

Ethan Dong