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


i'm learning python , i've made password locker that's supposed copy password clipboard. if doesn't find account looking for, it'll ask if want add password account, , update dictionary new items.

my problem dictionary updates every run, loses new items when run again. every run doesn't carry on next.

here's code:

#! python2 # password locker program in python  # dict store 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, first filename, second first command line arg.     # if argument entered less 2, print below     print "usage: python pw.py [acount] - copy account password"     sys.exit() # done sys.argv  account = sys.argv[1] # first command line argv account name. can use sys.argv[1], cryptic , confusing.  if account in passwords: # if account name (the key) in passwords     pyperclip.copy(passwords[account]) # pyperclip.copy() copies things. passwords[account] call value copied     print "password for", account, "has been copied." else:     print "would add password account?"     answer = raw_input("enter yes or no: ")     if answer.lower() == "yes":         print "enter password for", account, ". make hard!"         password = raw_input("password: ")         passwords[account] = password # way put is: password.update({account: password})         print "account , password added database!"     else:         print "done"  print passwords 

here's want do: if search account doesn't exist, ask me password, saves it. when run program again in new run, key , value there, , copied clipboard.

i want new items added passwords dict in program itself, , use reference next time around.

running mac os, using pycharm. using python 2.7

thanks

(of course, password on program isn't real passwords. duh)

python dictionaries don't persist longer execution of script. take python pickle module serializing dictionary , writing file on completion of program, deserializing , loading dictionary memory, file, on completion.

i'll note there several different other ways of learning , doing operation, pickle included in standard python 2.7, , is, in opinion, easiest learn. here simple tutorial.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -