简体   繁体   中英

Password Manager Replacing Passwords

I want to append the same website accounts. But it is Replacing Passwords when trying to save multiple accounts on the same website. What am I doing wrong? Here is my Code:

def save():
    websites = website_entry.get()
    email = user_entry.get()
    passwords = password_entry.get()
    new_data = {
        websites: {
            "Email": email,
            "Password": passwords,
        }
    }

if len(websites) == 0 or len(email) == 0 or len(passwords) == 0:
    messagebox.showinfo(title="Oops!", message="Please give all the required information's")
else:
    try:
        with open("data.json", "r") as data_file:
            data = json.load(data_file)
    except FileNotFoundError:
        with open("data.json", "w") as data_file:
            json.dump(new_data, data_file, indent=4)
    else:
        data.update(new_data)

        with open("data.json", "w") as data_file:
            json.dump(data, data_file, indent=4)
    finally:
        user_entry.delete(0, END)
        website_entry.delete(0, END)
        password_entry.delete(0, END)

The problem is that you are opening data.json in write versus append mode when you go to add new data to it:

with open("data.json", "w") as data_file:
    json.dump(data, data_file, indent=4)

Instead, you should be opening data.json in append mode to avoid overwriting the existing contents of the file:

with open("data.json", "a") as data_file:
    json.dump(data, data_file, indent=4)

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