我需要在每次修改字典时对其进行更新

时间:2018-06-24 14:54:13

标签: python python-3.x cmd argv

我制作了一个程序,用于存储,更新,删除不同帐户的密码。但是,尽管程序运行平稳,但是每当我重新启动程序时,PASSWORDS词典都会重置为程序中的值。每当我使用Windows CMD中的程序时,是否可以更新词典?

from sys import *
from pyperclip import *

if argv[1] == 'CamelCase':
    print('Entering program')
else:
    print('Enter correct password for program')
    exit()

PASSWORDS = {'email': 'F7minlBDDuvMJuxESSKHFhTxFtjVB6',
'blog': 'VmALvQyKAxiVH5G8v01if1MLZF3sdt',
'luggage': '12345'}


if len(argv) < 2:
    print('No account named')
    exit()

action = argv[2]
account = argv[3]
if len(argv) == 5:
    password = argv[4]
else:
    pass

if action == 'check':
    if account in PASSWORDS:
        print('The account exists.\nDo you want the password.\nEnter y / n')
        response = input()
        if response == 'y':
            copy(PASSWORDS[account])
            print('Password for ' + account + ' copied to clipboard.')
            exit()
        elif response == 'n':
            print('Closing the program')
            exit()
        else:
            print('Closing program due to invalid response')
            exit()
if action == 'copy':
    copy(PASSWORDS[account])
    print('Password for ' + account + ' copied to clipboard.')
    exit()
elif action == 'add':
    PASSWORDS[account] = password
    print('Password for ' + account + ' has been added')
    exit()
elif action == 'remove':
    PASSWORDS.pop(account)
    print('Password for ' + account + ' has been removed')
    exit()
elif action == 'update':
    PASSWORDS[account] = password
    print('Password for ' + account + ' has been updated')
    exit()

2 个答案:

答案 0 :(得分:0)

您需要使用file(例如:文本文件)或数据库。在此处存储您的值,然后从此处阅读。如果您要存储在关闭程序后不会被删除的数据,那么这就是关键。最简单的方法是使用文件。您可以阅读this documentation来了解如何在Python中使用files

答案 1 :(得分:-1)

您需要在此处检查答案: How to save a dictionary to a file?

如果您的密码列表确实很大,并且/或者要同时运行此代码,同时在更多实例中运行,则可以考虑使用某些数据库或其他后端。