[英]Getting TypeError when passing and setting command line argument in python
Okay so I want to add command-line arguments for four different files I have done it by the following code.好的,所以我想为四个不同的文件添加命令行参数,我通过以下代码完成了它。 (suggest if I have done anything wrong here) (建议我在这里做错了什么)
def parse_opt(known=False):
parser = argparse.ArgumentParser()
parser.add_argument(default=ROOT / 'courses.csv', help='path to course.csv file')
parser.add_argument(default=ROOT / 'students.csv', help='path to students.csv file')
parser.add_argument(default=ROOT / 'tests.csv', help='path to tests.csv file')
parser.add_argument(default=ROOT / 'marks.csv', help='path to marks.csv file')
parser.add_argument(default=ROOT / 'output.json', help='path to output.json file')
return parser
next I'm just testing it by calling the function as mentioned below:接下来我只是通过调用下面提到的函数来测试它:
opt=parse_opt()
#print(opt)
#json_data=prepareJsonData(opt)
#makeFile(json_data)
And I'm getting this error我收到了这个错误
python readFile.py 'C:/Users/Mudassir/Downloads/Example1/Example1/courses.csv','C:/Users/Mudassir/Downloads/Example1/Example1/students.csv','C:/Users/Mudassir/Downloads/Example1/Example1/tests.csv','C:/Users/Mudassir/Downloads/Example1/Example1/marks.csv', C:\Users\Mudassir\Desktop\hatchways\output.json
Traceback (most recent call last):
File "C:\Users\Mudassir\Desktop\hatchways\readFile.py", line 115, in <module>
opt=parse_opt()
File "C:\Users\Mudassir\Desktop\hatchways\readFile.py", line 107, in parse_opt
parser.add_argument(default=ROOT / 'courses.csv', help='path to course.csv file')
File "C:\Program Files\Python39\lib\argparse.py", line 1398, in add_argument
kwargs = self._get_positional_kwargs(*args, **kwargs)
TypeError: _get_positional_kwargs() missing 1 required positional argument: 'dest'
Any idea what I'm doing wrong here?知道我在这里做错了什么吗?
I tried searching internet but not much info is available there.我尝试搜索互联网,但那里没有太多信息。
you are missing the nargs
parameter from the add_argument
call.您缺少add_argument
调用中的nargs
参数。
something like the following should give you optional positional command line arguments.类似下面的内容应该为您提供可选的位置命令行参数。
def parse_opt(known=False):
parser = argparse.ArgumentParser()
parser.add_argument('c', nargs='?', default='courses.csv', help='path to course.csv file')
parser.add_argument('s', nargs='?', default='students.csv', help='path to students.csv file')
parser.add_argument('t', nargs='?', default='tests.csv', help='path to tests.csv file')
parser.add_argument('m', nargs='?', default='marks.csv', help='path to marks.csv file')
parser.add_argument('o', nargs='?', default='output.json', help='path to output.json file')
return parser
secondly, when making the command line call, you should not use commas to separate the command line arguments, instead:其次,在进行命令行调用时,不应使用逗号分隔命令行参数,而是:
python readFile.py /c/courses.tsv
the output for printing the args
after making a call t parser.parse_args()
is:调用 t parser.parse_args()
后打印args
的输出是:
Namespace(c='C:/courses.tsv', m='marks.csv', o='output.json', s='students.csv', t='tests.csv')
notice how it only took my first supplied argument, and defaulted the rest to values provided in the add_argument
call.请注意它是如何只使用我提供的第一个参数,并将其余参数默认为add_argument
调用中提供的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.