Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Saving Data outside of program [closed]

Tags:

python

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?

like image 806
HAL 9000 Avatar asked May 19 '26 15:05

HAL 9000


2 Answers

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

like image 187
jabaldonedo Avatar answered May 22 '26 05:05

jabaldonedo


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.

like image 36
FastTurtle Avatar answered May 22 '26 06:05

FastTurtle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!