473,473 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to save a dictionary over multiple uses

3 New Member
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
1 1502
dwblas
626 Recognized Expert Contributor
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

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

Similar topics

3
by: Erik Lechak | last post by:
Hello all, I wrote the code below. It is simply a dictionary that uses regular expressions to match keys. A quick look at _test() will give you an example. Is there a module that already...
1
by: San71 | last post by:
I'm writing an application which creates a rather large dictionary (> 500 MB) from MySQL data. It is a dictionary which uses integers, tuples, and datetime instances as keys. I've tried shelve...
12
by: Daniel Walzenbach | last post by:
Hi, I have a Website which allows users to input data. After they finished entering data they can click a button to save their input. Problem now is, that I have no possibility to visualize that...
1
by: SpreadTooThin | last post by:
I have a dictionary that looks like: a0,b0,c0,d0,e0 a1,b1,c1,d1,e1 .... .... Currently I consider (Class Tag) to be the key and (Class Value) to be the value. However I now need to use ...
6
by: globalrev | last post by:
i extract info from one file and put it into a dictionary. i want to save that dictionary for later use, how do i do that? might save a list of dictionaries or a list of classobjects too if there...
6
by: Tony | last post by:
Hello! When you are using a Dictionary object what type is to possible to have on the key? //Tony
2
by: Rowan | last post by:
I have a form that has several multiple select menus. I would like to save the multiple selections but be able to show them as selected items when the form data is updated. <select...
19
Brilstern
by: Brilstern | last post by:
Q: I am getting "invalid use of null" error. Whats wrong?!? The goal in this command is to first create a individual report "rptECR" for each SSN in tblBand Members. Next, save each report as a...
5
by: tomlecoq | last post by:
Hello, I'm a bit of a newby here, but i have a small problem and i hope someone can help me. We have a small access database to record sales for small fairs. We used to hook up 3 laptops to a...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.