Henry Lee
Henry Lee

Reputation: 831

How to get dictionary to save newly added item for the next run?

I'm learning Python and I've made a password locker that's supposed to copy the password to your clipboard. If it doesn't find the account your looking for, it'll ask if you want to add a password for that account, and update the dictionary with the new items.

My problem is that the dictionary updates for every run, but loses the new items when I run it again. So every run doesn't carry on to the next.

Here's the code:

#! python2
# a password locker program in Python

# Dict to store the account: password
PASSWORDS = {"email": "password",
             "blog": "password",
             "luggage": "password",
             "house": "password"}

import sys, pyperclip # Handles command line arguments
if len(sys.argv) < 2: # sys.argv takes 2 arguments, the first is the filename, the second is the first command line arg.
    # if the argument entered is less than 2, print the below
    print "Usage: Python pw.py [acount] - copy account password"
    sys.exit() # done with sys.argv

account = sys.argv[1] # first command line argv is the account name. We can just use sys.argv[1], but that would be cryptic and confusing.

if account in PASSWORDS: # if the account name (the key) is in PASSWORDS
    pyperclip.copy(PASSWORDS[account]) # pyperclip.copy() just copies things. PASSWORDS[account] will call the value to be copied
    print "Password for", account, "has been copied."
else:
    print "Would you like to add a password for this account?"
    answer = raw_input("Enter YES or NO: ")
    if answer.lower() == "yes":
        print "Enter your password for", account, ". Make it hard!"
        password = raw_input("Password: ")
        PASSWORDS[account] = password # another way to put it is: PASSWORD.update({account: password})
        print "Account and password added to database!"
    else:
        print "DONE"

print PASSWORDS

Here's what I want it to do: If I search an account that doesn't exist, ask me for the password, then saves it. When I run the program again in a new run, that key and value is there, and would be copied to my clipboard.

I want the new items to be added to the PASSWORDS dict in the program itself, and use that as reference the next time around.

Running Mac OS, using Pycharm. Using Python 2.7

Thanks

(OF COURSE, THE PASSWORD ON THE PROGRAM ISN'T MY REAL PASSWORDS. Duh)

Upvotes: 2

Views: 161

Answers (1)

jwin
jwin

Reputation: 41

Python dictionaries don't persist any longer than the execution of the script. Take a look into the python pickle module for serializing your dictionary and writing it to a file on completion of the program, then deserializing and loading the dictionary back into memory, from the file, on completion.

I'll note that there are several different other ways of learning and doing this operation, but pickle is included in standard python 2.7, and is, in my opinion, easiest to learn. Here is a simple tutorial.

Upvotes: 4

Related Questions