So I know the .append function adds elements to a list. But that only works as long as the script is running. If I rerun the file, the list is back to its previous state with no changes made. Is there a way to save the changes?
Asked
Active
Viewed 446 times
0
-
2You could write it to a file and then read it. File I/O is a standard way to maintain state between runs. – John Coleman Aug 05 '21 at 11:16
-
2Either save it to a database or a text file, or change the python code as you run it – Aug 05 '21 at 11:17
1 Answers
0
You can save the list to a file after everything is done:
Here for a+
mode:
All file handling modes <---Here
a+
: Open for reading and appending (writing at end of file). The file is created if it does not exist.
with open('test_file','a+') as file:
lst=[...]
....
file.write(lst)