Python 列出图像路径-';名称空间';对象没有属性'__获取项目';

Python 列出图像路径-';名称空间';对象没有属性'__获取项目';,python,image,parsing,path,argparse,Python,Image,Parsing,Path,Argparse,我尝试返回图像路径,如下所示: from imutils import paths import argparse # create parser parser = argparse.ArgumentParser() # define the command-line options parser.add_argument('--dataset', required=True,help='path to the dataset') # read the command-line argumen

我尝试返回图像路径,如下所示:

from imutils import paths
import argparse

# create parser
parser = argparse.ArgumentParser()
# define the command-line options
parser.add_argument('--dataset', required=True,help='path to the dataset')
# read the command-line arguments and interpret them
args = parser.parse_args()

imagePath = paths.list_images(args['dataset'])

print imagePath
但是,出现以下错误:

Traceback (most recent call last):
  File "test.py", line 11, in <module>
    imagePath = paths.list_images(args['dataset'])
TypeError: 'Namespace' object has no attribute '__getitem__'
知道我做错了什么吗

谢谢。

这应该行得通

imagePath = paths.list_images(args.dataset)
或者,如果您出于某种原因需要dict:

imagePath = paths.list_images(args.__dict__['dataset'])

我执行了以下操作以使程序正常工作(注意
vars()
):


args
argparse.Namespace
对象,而不是字典。您需要一个属性。请打印(args),以便更清楚地了解它是什么。感谢您的友好回复。对于第一条语句,我得到了以下错误:“文件”test.py,第11行,在imagePath=path.list_图像(args.dataset)AttributeError:“dict”对象没有属性“dataset”。对于第二条语句,我得到了以下错误:“文件”test.py,第11行,在imagePath=path.list_图像(args.\u dict_u['dataset']))AttributeError:'dict'对象没有属性'dict'@Simplication re:#1-这很奇怪,argparse的版本是什么?您确定没有在那里使用VAR,因为在初始问题报告中,报告的对象类型不是dict,而是Namespace,re:#2-从错误消息中,您似乎已经将
.dict
放在了
。\uuuu dict
imagePath = paths.list_images(args.__dict__['dataset'])
from imutils import paths
import argparse

# create parser
parser = argparse.ArgumentParser()
# define the command-line options
parser.add_argument('--dataset', required=True,help='path to the dataset')
# read the command-line arguments and interpret them
args = vars(parser.parse_args())

print args

imagePath = paths.list_images(args['dataset'])

for image in imagePath:
    print image