如何在参数解析器中传递图像目录的路径?

2024-04-25 17:41:20 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: py图像路径文件夹add解析器参数方式
1条回答
网友
1楼 · 发布于 2024-04-25 17:41:20

试试这个:

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

相关问题 更多 >