3

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?

HAL 9000
  • 61
  • 1
  • 6

5 Answers5

7

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

jabaldonedo
  • 25,822
  • 8
  • 77
  • 77
  • that's the best possible answer. If you need something basic, use shelve. If you need something bigger, look at RDBMSes (SQL). – ducin Jun 18 '13 at 21:39
  • @tkoomzaaskz: If you need something even more basic, where all of the values are strings—as in the OP's example—you shouldn't use `shelve`; just use `dbm`/`anydbm`. There's no reason to go to the expense of pickling and unpickling each value string, or to make the database file unreadable with normal dbm tools, when you can just store the strings directly. – abarnert Jun 24 '13 at 23:13
2

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.

FastTurtle
  • 2,301
  • 19
  • 19
0

Learn how to read and write Files in python, and just store the username and password in a text file.

Elias Benevedes
  • 363
  • 1
  • 8
  • 26
  • 1
    Encrypting passwords is a bad idea. If you need any security at all, you should be hashing them and not storing them at all. If you don't need any security, you're just making things more complicated and providing a false sense of security. – abarnert Jun 18 '13 at 21:30
  • plain files is a bad idea. Use pickle/dbm/shelve. – ducin Jun 18 '13 at 21:38
  • 2
    He did say he was making it 'For fun'. I don't think he cares much about safety of passwords. – Elias Benevedes Jun 18 '13 at 21:42
  • @tkoomzaaskz: Why? Plain files are exactly as (in)secure as `dbm`/etc. Using `dbm` makes things slightly simpler, and it's a useful thing for a novice to learn about, and so on, so it's probably worth doing—but that doesn't mean that plain files are a bad idea! And it's certainly not worth downvoting a perfectly decent answer for. – abarnert Jun 24 '13 at 23:16
  • @EliasBenevedes: If your comment about safety of passwords is in response to my comment: You originally suggested storing encrypted passwords in a text file, and my comment was explaining why (if it's "for fun" and security isn't an issue) you should just store plaintext passwords. Which is what your edited answer now says. – abarnert Jun 24 '13 at 23:17
  • @abarnert Well, in my opinion, if a platform provides some features that solve basic programming problems, developer should use them and should not reinvent the wheel. Implementing a hash-file may contain bugs and built-in modules guarantee quality. You don't have to agree with me, but that's what I think actually. – ducin Jun 25 '13 at 09:10
0

There are many options. You can start with learning how native Python's input and output works.

Then you could try sqlite.

And finally use a proper database (MySQL, PostgreSQL, MongoDB, etc.).

freakish
  • 54,167
  • 9
  • 132
  • 169
0

The shelve module, as suggested by jabaldonedo, is a drop-in replacement for dictionaries. However, when your values are all strings, even shelve is overkill; you can just use dbm.

Either way, you only need to change one line:

d = {} # in-memory, non-persistent dict
d = shelve.open('passwd.db', 'c') # persistent dict that can store anything
d = dbm.open('passwd.db', 'c') # persistent dict that only stores strings

The advantage of dbm is that you can use other tools to read (and edit) the database. For example, if you do this:

d['abc'] = 'def'

With dbm, the database will hold the string 'def'. With shelve, it will hold something like '\x80\x03X\x03\x00\x00\x00defq\x00.'.

abarnert
  • 354,177
  • 51
  • 601
  • 671