python - Argparse 确认

标签 python amazon-s3

我在我的程序中使用 aprgeparse 创建一个参数,允许用户从 amazon s3 存储桶中删除文件。我以这种方式创建它:

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

最佳答案

我有同样的问题(在不同的 CLI 中)。我建议您使用给定的答案 here .看看詹姆斯给出的答案。它相当容易理解。像这样使用它:

if args.remove:
    b = Bucket(conn,default_bucket)
    k = Key(b)
    if b.get_key(args.remove):
        if yes_no('Are you sure: ')
            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

关于python - Argparse 确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28462962/

相关文章:

python - 如何使用 GDB 调试 asyncio 协程?

python - 为什么变量1 + =变量2要比变量1 =变量1 +变量2快得多?

javascript - 在将文件上传到 S3 之前重命名文件

amazon-web-services - S3文件夹不断出现和消失

python - 在对数组进行操作后将 NaN 值替换为零

python - 向量化形式以获取大于引用的元素数

amazon-web-services - 如何创建/写入文本文件而不是上传到 S3 存储桶

amazon-web-services - 如何从亚马逊的请求者支付桶下载数据?

python - MySQL:如何确保始终选择唯一记录

scala - 如何在Spark Shell中将s3与Apache Spark 2.2一起使用