对 argparse 感到困惑
Confused about argparse
我在理解 argparse 的工作原理时遇到了一些困难,我已经梳理了文档,但仍然难以理解。
def arguments():
parser = argparse.ArgumentParser(description='Test..')
parser.add_argument("-i", "--input-file", required=True, help="input file name")
parser.add_argument("-o", "--output-file", required=True, help="output file name")
parser.add_argument("-r", "--row-limit", required=True, help="row limit to split", type=int)
args = parser.parse_args()
is_valid_file(parser, args.input_file)
is_valid_csv(parser, args.input_file, args.row_limit)
return args.input_file, args.output_file, args.row_limit
def is_valid_file(parser, file_name):
"""Ensure that the input_file exists"""
if not os.path.exists(file_name):
parser.error("The file {} does not exist".format(file_name))
sys.exit(1)
def is_valid_csv(parser, file_name, row_limit):
"""
Ensure that the # of rows in the input_file
is greater than the row_limit.
"""
row_count = 0
for row in csv.reader(open(file_name)):
row_count += 1
if row_limit > row_count:
parser.error("More rows than actual rows in the file")
sys.exit(1)
上面的代码工作正常,但是当我删除第 5 行的“--row-limit”后,我得到一个
Traceback (most recent call last):
File ".\csv_split.py", line 95, in <module>
arguments = arguments()
File ".\csv_split.py", line 33, in arguments
is_valid_csv(parser, args.input_file, args.row_limit)
AttributeError: 'Namespace' object has no attribute 'row_limit'
为什么删除“--row-limit”会出现此错误?
args = parser.parse_args()
实际上为每个 parser.add_argument
调用添加一个属性到命名空间 args
。属性的名称是从您的参数名称生成的,这里 --row-limit
被转换为 row_limit
因为变量名称中不能有破折号。有关详细信息,请参阅 argparse documentation。
因此,当您调用 parser.add_argument(..., "--row-limit", ...)
时,它会在您调用 parse_args()
后创建 args.row_limit
。正如 Amadan 提到的,您稍后会在代码中使用 args.row_limit
。但是,如果从解析器中删除 --row-limit
参数,属性 row_limit
将不存在于 args
.
中
我在理解 argparse 的工作原理时遇到了一些困难,我已经梳理了文档,但仍然难以理解。
def arguments():
parser = argparse.ArgumentParser(description='Test..')
parser.add_argument("-i", "--input-file", required=True, help="input file name")
parser.add_argument("-o", "--output-file", required=True, help="output file name")
parser.add_argument("-r", "--row-limit", required=True, help="row limit to split", type=int)
args = parser.parse_args()
is_valid_file(parser, args.input_file)
is_valid_csv(parser, args.input_file, args.row_limit)
return args.input_file, args.output_file, args.row_limit
def is_valid_file(parser, file_name):
"""Ensure that the input_file exists"""
if not os.path.exists(file_name):
parser.error("The file {} does not exist".format(file_name))
sys.exit(1)
def is_valid_csv(parser, file_name, row_limit):
"""
Ensure that the # of rows in the input_file
is greater than the row_limit.
"""
row_count = 0
for row in csv.reader(open(file_name)):
row_count += 1
if row_limit > row_count:
parser.error("More rows than actual rows in the file")
sys.exit(1)
上面的代码工作正常,但是当我删除第 5 行的“--row-limit”后,我得到一个
Traceback (most recent call last):
File ".\csv_split.py", line 95, in <module>
arguments = arguments()
File ".\csv_split.py", line 33, in arguments
is_valid_csv(parser, args.input_file, args.row_limit)
AttributeError: 'Namespace' object has no attribute 'row_limit'
为什么删除“--row-limit”会出现此错误?
args = parser.parse_args()
实际上为每个 parser.add_argument
调用添加一个属性到命名空间 args
。属性的名称是从您的参数名称生成的,这里 --row-limit
被转换为 row_limit
因为变量名称中不能有破折号。有关详细信息,请参阅 argparse documentation。
因此,当您调用 parser.add_argument(..., "--row-limit", ...)
时,它会在您调用 parse_args()
后创建 args.row_limit
。正如 Amadan 提到的,您稍后会在代码中使用 args.row_limit
。但是,如果从解析器中删除 --row-limit
参数,属性 row_limit
将不存在于 args
.