By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,373 Members | 1,792 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,373 IT Pros & Developers. It's quick & easy.

How to save a dictionary over multiple uses

P: 3
I am creating a login script and I store the usernames and passwords in a dictionary. The problem is though, when I open it a second time it doesn't store the users that have already been registered. I know this is normal but is there a way to get round that? Perhaps using a file?

Note: I asked this on Stackoverflow and got no answer so i am re asking. That is why I am trying to use Pickle.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. import os
  2. import sys
  3. users={}
  4. status=""
  5.  
  6. def login():
  7.     status=raw_input("Are you a new user?")
  8.     if status=="y"
  9.         createnewuser=raw_input("Create username: ")
  10.         if createnewuser in users:
  11.             print "User already exists!"
  12.         else createpsswrd=raw_input("Create new password")
  13.             users[createnewuser]=createpsswrd 
  14.             print "Register successful!"
  15.     elif status == "n":
  16.         login=raw_input("Username: ")
  17.         passw=raw_input("Password: ")
  18.         if login in users and users[login]==passw:
  19.             print "Login successful!"
  20.             os.system("python file.py")
  21.             return
  22.         else:
  23.             print "Username and password do not match."
  24. try:
  25.     with open('file') as infile:
  26.         cPickle.load(infile)
  27. except:
  28.     users = {}
  29.  
  30. while status != "q":
  31.     login()
  32.  
  33. with open('file') as outfile:
  34.     cPickle.dump(users, outfile)
Results: I get through the entire script with no errors but the outfile file has nothing written on it. The dictionary doesn't save across sessions. I am using Raspian on a raspberry Pi 2 if that matters.
Sep 16 '15 #1

✓ answered by dwblas

You don't catch the input from cpickle.load(). Simple example at https://wiki.python.org/moin/UsingPickle Note also that you open the output file as a read/input file (the default), when it should be "wb", write, binary. You may have not gotten any responses because of the title. It looks like a code snippet (a Howto), not a question, and if you already know "How to save a dictionary" then you don't even look at the thread (as I almost did here).

Share this Question
Share on Google+
1 Reply


Expert 100+
P: 618
You don't catch the input from cpickle.load(). Simple example at https://wiki.python.org/moin/UsingPickle Note also that you open the output file as a read/input file (the default), when it should be "wb", write, binary. You may have not gotten any responses because of the title. It looks like a code snippet (a Howto), not a question, and if you already know "How to save a dictionary" then you don't even look at the thread (as I almost did here).
Sep 17 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.