天天看點

《Python程式設計快速上手》8.9.1實

#!python3
#運作 py mcb.pyw save spam ,剪貼闆内容就關鍵字spam儲存
#py mcb.pyw spam 加載spam 對應内容到剪貼闆
#py mcb.pyw list 關鍵字清單到剪貼闆

#擴充多重剪貼闆:
#增加delete <keyword>,從shelf中删除相應鍵值
#delete all 清空
import sys,pyperclip,shelve

mcbShelf=shelve.open(r'I:\mcb')

#save clipboasr content:
if len(sys.argv) == 3:
    if sys.argv[1].lower()=='save' and sys.argv[2].lower() != 'all':
        mcbShelf[sys.argv[2]] = pyperclip.paste()
    elif sys.argv[1].lower()=='delete' and sys.argv[2].lower()=='all':
        mcbShelf.clear()
    elif sys.argv[1].lower()=='delete' and sys.argv[2] in mcbShelf:
        del mcbShelf[sys.argv[2]]    
elif len(sys.argv) == 2:
    #List keywords and load content.
    if sys.argv[1]=='list':
        pyperclip.copy(str(list(mcbShelf.keys())))
    elif sys.argv[1] in mcbShelf:
        pyperclip.copy(mcbShelf[sys.argv[1]])
    else:
        print("參數錯誤")
else:
    print('wrong')

mcbShelf.close()           

複制