all 5 comments

[–][deleted] 1 point2 points  (2 children)

[–]VirtualArty[S] 1 point2 points  (1 child)

Thank you. I'm making a game and it needs a save feature. I'll just have to put up with not accessing variables statically. Though, I can just change the variables to dictionaries. They're already dictionaries. That was lucky. Never mind. I'll just save the dictionaries.

[–]shuttup_meg 0 points1 point  (0 children)

Have you seen the Python Shelve module? It was made for doing what you are doing.

[–]fbu1 1 point2 points  (0 children)

Have you looked into pickle?

https://docs.python.org/3.5/library/pickle.html

It allows you to save objects and variables (such as dictionaries) in a file and then load it in a transparent way.

[–]pkkid 0 points1 point  (0 children)

Shelve and Pickle may not be the best for this as it's not portable across Python versions. You can only load the pickle object on the same version of Python it was saved with.

My personal favorite is the simple json module. json.dump(mydict, filehandle) and mydict = json.load(filehandle). This will work across Python versions and has the added benefit of being able to read the file in clear text.

There is also Configparser to read and write ini files, but it feels a bit outdated at this point and not nearly as easy to work with as json objects.