实践项目:
8.9.1 扩展多重剪贴板扩展本章中的多重剪贴板程序,增加一个delete 命令行参数,它将从shelf中删除一个关键字。然后添加一个delete命令行参数,它将删除所有关键字。
这个“扩展多重剪贴板”项目的代码,只写了input()版本的,没有写用命令行和sys.argv版本的,因为前面的一篇关于“多重剪贴板”项目文章(https://blog.csdn.net/okbitcoin/article/details/107575302), 已经写了sys.argv和input()两个版本的。
下面是代码:
import shelve,pyperclip
s=shelve.open('1')
lst=list((input(),input()))
if len(lst)==2 and lst[0].lower()=='save':
s[lst[1]]=pyperclip.paste()
elif len(lst)==2 and lst[0].lower()=='delete' and lst[1] in list(s.keys()):
del s[lst[1]]
elif len(lst)==2 and lst[0] in list(s.keys()):
print(s[lst[0]])
pyperclip.copy(s[lst[0]])
elif len(lst)==2 and lst[0].lower()=='delete' and lst[1].lower()=='all':
s.clear()
else:
print(list(s.keys()))
pyperclip.copy(str(list(s.keys())))