我怎么能在 argparse 指向路径名的情况下默认为空?
How could I default to nothing with argparse pointing to a pathname?
目前,我正在使用argparse
输出以下文件:
import argparse
....
parser = argparse.ArgumentParser(description='do cool stuff')
parser.add_argument('--output_text_file',
default="outputs/output_file1.txt",
help='file path for the output text file')
这已经是一个可选参数。但是,我希望默认选项为 do not output anything
。如果用户想要文件输出,他们可以使用上面的路径名调用参数,例如`--output_text_file "outputs/my_text.txt"
执行此操作的正确方法通常是什么?
我怀疑如果我使用:
default=""
上面会有错误
很简单,省略默认即可:
parser.add_argument('--output-text-file')
在args中,检查是否output_text_file is None
。 名称将始终存在。
您可以将默认值设置为os.devnull,它是一个跨平台值,对应于空设备的文件路径
parser.add_argument('--output_text_file',
default=os.devnull,
help='file path for the output text file')
目前,我正在使用argparse
输出以下文件:
import argparse
....
parser = argparse.ArgumentParser(description='do cool stuff')
parser.add_argument('--output_text_file',
default="outputs/output_file1.txt",
help='file path for the output text file')
这已经是一个可选参数。但是,我希望默认选项为 do not output anything
。如果用户想要文件输出,他们可以使用上面的路径名调用参数,例如`--output_text_file "outputs/my_text.txt"
执行此操作的正确方法通常是什么?
我怀疑如果我使用:
default=""
上面会有错误
很简单,省略默认即可:
parser.add_argument('--output-text-file')
在args中,检查是否output_text_file is None
。 名称将始终存在。
您可以将默认值设置为os.devnull,它是一个跨平台值,对应于空设备的文件路径
parser.add_argument('--output_text_file',
default=os.devnull,
help='file path for the output text file')