值= self.cache [key]使用Python搁置时出现键错误

时间:2018-08-15 03:47:38

标签: python keyerror shelve

代码如下:

#! python3
# Locker
'''Usage: Window + R: locker <site>
                      locker update <site> <password>
                      locker delete <site>
                      locker all '''

import sys,shelve,pyperclip
getPass = shelve.open('puppy')
if sys.argv[1] != 'all':
    pyperclip.copy(getPass[sys.argv[1]])
    print('Password of',sys.argv[1],'copied to clipboard.')
elif sys.argv[1] == 'update':
    getPass[sys.argv[2]]=sys.argv[3]
    print('Update password for',sys.argv[2],'in database.')
elif sys.argv[1] == 'delete':
    del getPass[sys.argv[2]]
    print('Password for',sys.argv[2],'removed from database.')
elif sys.argv[1] == 'all':
    print('Current Database:'.center(30))
    for site,key in list(getPass.items()):
        print(str(site).ljust(20,'.'),end='')
        print(key.ljust(20))

getPass.close()

1 /它做什么?好吧,你几乎可以说出来。我制作了一个小程序来保存密码。每当我想快速获取某个站点的密码时,我只需要打开“运行”并键入“ locker(site)”,密码就会被复制到剪贴板中,而我只需将其粘贴在密码字段中即可。还添加了其他任务,例如更新密码或删除密码,您可以在代码中看到它们的语法。 (我确实创建了一个.bat文件来简化该程序的运行) 2 /问题?当我尝试使用更新或删除语法时,它抛出KeyError,但我不知道为什么:

Traceback (most recent call last):
  File "C:\Users\pminh\AppData\Local\Programs\Python\Python37-32\lib\shelve.py", line 111, in __getitem__
    value = self.cache[key]
KeyError: 'update'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\pminh\AppData\Local\Programs\Python\Python37-32\locker.py", line 12, in <module>
    pyperclip.copy(getPass[sys.argv[1]])
  File "C:\Users\pminh\AppData\Local\Programs\Python\Python37-32\lib\shelve.py", line 113, in __getitem__
    f = BytesIO(self.dict[key.encode(self.keyencoding)])
  File "C:\Users\pminh\AppData\Local\Programs\Python\Python37-32\lib\dbm\dumb.py", line 153, in __getitem__
    pos, siz = self._index[key]     # may raise KeyError
KeyError: b'update'
Press any key to continue . . .

0 个答案:

没有答案