0
How to save/store changes to Python Script before being closed?
I am writing a script that lets the user modify a list. The list by default has items in it. But the user can add or remove any item. This works fine while the script is running. But as soon as you close it.. it reverts to the defaults and removes all of the users changes. So how do you tell python to save those changes before closing?
5 Respuestas
+ 1
Serialize to disk or to a database (which could be on disk). Then read it when you load your script.
The simplest thing would be to save it as a CSV and then read each element of the file in your script at script startup.
0
Thanks for replying.. but I'm still confused. I had in mind possibly just saving the list to a .txt file and having the script use the list directly from there... is that what you mean by serialize to a database or disk?
Also this seems like the kind of thing that should be included in a module somewhere or a simple function that could be added to the end of a script.
0
Yes. Serialize to disk means save to an external file (text or otherwise). It is fairly common and I'm sure you can find a module or some common coffee that will do the main work for you.
0
# contents of test.txt [1,2,3,4]
with open('test.txt','r+') as my_file:
active_list = eval(my_file.readline())
print(type(active_list)) # type list
0
Thank you, gentlemen.