0

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

Liam F-A
  • 135
  • 11
  • What you are describing is a data file, not an application – C8H10N4O2 Mar 27 '18 at 13:36
  • Read up on data serialization for Python, there are plenty of options, like [`pickle`](https://docs.python.org/3/library/pickle.html)/[`shelve`](https://docs.python.org/3/library/shelve.html#module-shelve) (potentially unsafe), [JSON](https://docs.python.org/3/library/json.html), [YAML](http://pyyaml.org/), [XML](https://docs.python.org/3/library/xml.html), [Protocol Buffers](https://developers.google.com/protocol-buffers/) (more intended for network maybe but still), ... – jdehesa Mar 27 '18 at 13:44

2 Answers2

2

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.

mr_mo
  • 1,401
  • 6
  • 10
  • Worth noting that `pickle` can be unsafe and you shouldn't load untrusted data with it as it may run malicious code. – jdehesa Mar 27 '18 at 13:46
  • @jdehesa Correct, you might want to encrypt it after saving. – mr_mo Mar 27 '18 at 13:48
  • While `pickle` is used to store variables and objects in Python, I doubt this is what the OP wants or needs. It is rather a question about storing data to a file, using `open` (maybe formatted as json or XML or something) however badly worded. – JohanL Mar 27 '18 at 15:28
  • @mr_mo Ok, I am saving large amounts of data and I think I would be better off using XML but how could I do it so that when the user clicks on the project file icon, it opens in my application, rather than in notepad ? – Liam F-A Mar 27 '18 at 18:33
0

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?

boucekv
  • 1,220
  • 2
  • 20
  • 47