首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >密码管理器替换密码

密码管理器替换密码
EN

Stack Overflow用户
提问于 2021-03-14 01:03:52
回答 1查看 62关注 0票数 0

我想附加相同的网站帐户。但在同一网站上保存多个帐户时,它会替换密码。我做错了什么?以下是我的代码:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2021-03-15 04:41:22

问题是,当您向data.json添加新数据时,您是以写入模式打开它,而不是以附加模式打开它:

代码语言:javascript
复制
with open("data.json", "w") as data_file:
    json.dump(data, data_file, indent=4)

相反,您应该以附加模式打开data.json,以避免覆盖文件的现有内容:

代码语言:javascript
复制
with open("data.json", "a") as data_file:
    json.dump(data, data_file, indent=4)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66616241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档