简体   繁体   中英

Add a new key : value inside a existing dictionary and save that using pickle and then load that dictionary in another python file

So basically, I am trying to create a password manager where I can save my email and password. first I created a dictionary and save it using pickle:

import pickle

passDB = {} # this is the dictionary
key = input("Email: ") # taking email
password = input("Password: ") # taking password
passDB[key]= password # assigning new email and password into the dictionary
pickle.dump( passDB, open( "passDB.p", "wb" ) ) # saving it as passDB.p

Saved it as add2db.py. Then I created a password manager in python file like this:

import pickle
passDB = pickle.load( open( "passDB.p", "rb"))
query1 = input("Search your email account to get password: ").lower()
if query1 in passDB.keys():
    print(passDB[query1])
else:
    print("Nothing such email in database")

Saved it as passman.py. So, the problem here is, Suppose I added a new email and password into the add2db.py file. It adds perfectly. When I try to search password by email in passman.py file, it shows the password perfectly. But again, when I try to add another email and password in add2db.py, it again adds successfully but removes the existing email and password I added at first. And there is no sign of first email and password when I try to lookup using passman.py Can anyone solve the following?

  1. I want a create add2db.py file that keeps adding new email and password to an existing dictionary. The dictionary must contain all previous email and passwords. So that I can look for all the email and password in passman.py

In add2db.py, you need to load your database, add the new email, and then dump it again. Replace passDB = {} with passDB = pickle.load( open( "passDB.p", "rb" ) ) . Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM