【问题标题】:how to pass path to an image directory in argument parser?如何在参数解析器中将路径传递到图像目录?
【发布时间】:2020-02-24 15:32:39
【问题描述】:

我有一个包含一组图像的文件夹。我如何将它的路径传递给我以下列方式构造的参数解析器:

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", required=True, help="path to images directory")
args = vars(ap.parse_args())

我要经过的路径是home/downloads/images

目前我收到此错误:

usage: detect.py [-h] -i IMAGES
detect.py: error: the following arguments are required: -i/--images

【问题讨论】:

  • 你没有设置标志。如果是,请提供脚本崩溃时的 shell。
  • @HampusLarsson 设置标志是什么意思?你能根据问题举个例子吗>
  • 我也遇到了同样的问题。你能解决这个问题吗?

标签: python parsing arguments


【解决方案1】:

试试这个:

from argparse import ArgumentParser

parser = ArgumentParser(description='Demo: import images')
parser.add_argument('-i', '--images',
                    nargs='?',
                    required=True,
                    help='path to images directory')
args = parser.parse_args()

print("Bingo:", args.images)

这样调用:

./detect.py -i home/downloads/images

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 2013-08-26
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多