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:Instead, you should be opening
data.json
in append mode to avoid overwriting the existing contents of the file: