Menu Close

How to save an object to disk with Python?

Sometimes, we want to save an object to disk with Python.

In this article, we’ll look at how to save an object to disk with Python.

How to save an object to disk with Python?

To save an object to disk with Python, we can use the dill.dump method.

For instance, we write

import dill

dill.dump(company1, file = open("company1.pickle", "wb"))

company1_reloaded = dill.load(open("company1.pickle", "rb"))

to call dill.dumnp to save the company1 object to company1.pickle.

And then we load the company1.pickle file into an object with dill.load.

We install dill by running

pip install dill

Conclusion

To save an object to disk with Python, we can use the dill.dump method.

Posted in Python, Python Answers