Save instance of Python application

171 Views Asked by At

I wrote a Python application where the user can create projects, kind of like Word, Photoshop, Solidworks etc. Like for those applications, I need the user to save his work in a file thats specific to the program so that when he opens it, he continue to work on his project.

How can I do this ? Are there any existing libraries that can let me do this ?

Thanks.

Liam

2

There are 2 best solutions below

4
mr_mo On

The main tools that is suitable for you approach is pickle. It is native in Python and is a serialization method for all Python objects. See docs. The main problem with pickle is if you use it to save objects that holds a large amount of data. It will save the whole object and hence large files.

Alternative approaches are also very welcome and preferred when planning and implementing using a design pattern that is suitable for the job.

For instance, you can build configuration objects that tracks the work and save it into JSON formats.
Another language that is good for preferences and state capturing is XML.

However, in all the designed approaches you'll need to modify the code such that it can save and resume based on the defined settings, state and preferences.

All the above languages have libraries to support it, JSON is native.

0
boucekv On

You need to save those to the file

with open('Failed.py', 'w') as file:
    file.write('whatever')

Python Save to file

or to the database https://wiki.python.org/moin/DatabaseProgramming

or you can save the session

%save current_session ~0/
%save previous_session ~1/

How to save a Python interactive session?