Categories
Python Answers

How to save an object to disk with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *