我在我的程序中使用aprgeparse
創建參數,允許用戶從亞馬遜s3存儲桶中刪除文件。我以這種方式創建它:Argeparse確認
parser.add_argument("-r", "--remove",type = str, help= "Delete a file (enter full path to file)" , default = '')
我再檢查文件是否可用,如果是的,我將其刪除:
if args.remove:
b = Bucket(conn,default_bucket)
k = Key(b)
if b.get_key(args.remove):
b.delete_key(args.remove)
print (' {0} Has been removed'.format(args.remove.split("/")[-1]))
else:
print (" No files named '{0}' found ".format(args.remove.split("/")[-1]))
print (' Please check the bucket path or file name. Use -l command to list files')
我想知道是否有辦法,我可以提示用戶是否他真的希望用戶刪除通常是刪除文件的情況下的文件(每個程序都這樣做)。這樣
>python myprog.py -r foo.txt
>Are your sure [Y/N] : Y
>File deleted
我已經解決了這個問題。但是使用了我自己的非常「嚴格編碼」的解決方案。這樣好多了,我可以在很多地方使用它。謝謝! – letsc 2015-03-02 04:26:54