Python: saving objects and using pickle. extension of filename

22,102

Solution 1

Depends on what you want to do with the file.

filename should be sufficient.

And don't use object as an identifier. It shadows the builtin object.

Solution 2

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

Solution 3

One more thing you should pay attention to:

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

Share:
22,102
Peterstone
Author by

Peterstone

I am Electronic Engineer I am interesting in politics I am interested in business I am interested in artificial intelligent I am interested in learning productivity techniques based on the synergy of teams I am interested in learning about daily synergy way of working and living. I am interested in create a synergy community I am interested in tools to create agents to collect and select information by exploring the web or chatting with people. I am interested in FPGAs implementations of Neural Networks

Updated on July 16, 2022

Comments

  • Peterstone
    Peterstone almost 2 years

    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!