python - How to I modify an external list? -


i'm trying code thing in python, , have bumped road block. have list, , want modify it, , keep changes after program exited. have no clue start.

essentially, want list can accessed , modified, , saved , not reset after thing using closed.

edit: here idea; there list have, let's call basket. want code program add , remove 'basket', , keep changes after close program. hope clarifies... , tried using global, got funny, deleted version.

you can use json, pickle, or similar library write data file , read , store in list. create file save.json in current working directory contains json-information list. notice list contain numbers added before, doesn't "forget" added list since it's saved in file.

import os import json  foo = [1,2,5,9] #default  #if safe file exists, load contents foo list if os.path.isfile("save.json"):      open("save.json", "r") file:         foo = json.loads(file.read())  foo.append(int(input("enter number add foo"))) print(foo)  #write foo list file open("save.json", "w") file:      file.write(json.dumps(foo)) 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -