I am somewhat new to python, and I was making a username+password login system for fun. I am using a dictionary to store the username+password. I am going to make it so you can add an account to the dictionary, and I want some way to save the dictionary for the next time the program runs. How would I do this?
There are many option for persisting data, one easy way is using shelve
You can save data using:
>>> import shelve
>>> data = {'foo':'foo value'}
>>> d = shelve.open('myfile.db')
>>> d['data'] = data
>>> d.close()
Then you can recover your data easily:
>>> import shelve
>>> d = shelve.open('myfile.db')
>>> data = d['data']
>>> d.close()
Other options are using files, CPickle databases like SQLite, MySQL, etc
Depending on your needs, you can either save the information to a text file or use a database. Saving to a text file doesn't require any encoding, however two popular formats/libraries for python are json and pickle. If you want to use a database instead I would recommend looking at either mysql or sqlite.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With