Python argparse 输入文件错误
Python argparse input file error
我见过类似的问题,但没有适用于这种情况的解决方案。我有一个 argparser 函数:
def IO_fileParse():
parser = argparse.ArgumentParser();
parser.add_argument("-i", dest="input_file", help="no file with this name")
args = parser.parse_args();
input_file = os.path.join(os.getcwd(),args.input_file)
if not os.path.exists(input_file):
print(" ------------------------------------------------");
print("Error: -input_file Cannot find input file with that name.");
print(" ------------------------------------------------");
parser.print_help();
sys.exit();
我想说"if you can't find a file with the -i name in the directory, print the help and exit the script."
实际发生的情况是,这似乎用输入文件的名称创建了一个空文件?
如何将上面的代码更改为 "if -i file does not exist, tell user this file does not exist"(并且不要使用不存在的文件名创建一个空文件)。
我也试过 os.path.isfile 和 os.path.exists,但都没有给我想要的错误?...我也试过 args.input_file [=] =12=]
- 去掉分号。他们为什么在那里?
您没有正确访问变量。
您将其存储在名为 -i 的变量中。尝试调用它 -input_file,删除 dest,然后通过 args.input_file
访问它
os.getcwd(),会给你工作目录,而不是当前路径。获取当前路径os.path.dirname(os.path.abspath("__file__"))
我不知道你说得对不对,但是我建议的结构用法是 python test.py -input_file filename
编辑:您可以通过 parser.add_argument("-i", "--input_name")
提供 shorthand 选项和完整表格
这段代码对我有用
def IO_fileParse():
parser = argparse.ArgumentParser()
parser.add_argument("-input_file", help="no file with this name")
args = parser.parse_args()
input_file = os.path.join(os.getcwd(),args.input_file)
print(input_file)
if not os.path.exists(input_file):
print(" ------------------------------------------------")
print("Error: -input_file Cannot find input file with that name.")
print(" ------------------------------------------------")
parser.print_help()
sys.exit()
IO_fileParse()
除了小的调整,我没有发现你的脚本有问题。这是我的测试:
In [52]: p=argparse.ArgumentParser()
In [53]: p.add_argument('-i','--input-file')
...
没有标记选项,默认为None
,路径连接不起作用。应该给它一个有效的默认值或使用 if args.input_file is not None:
测试。
In [54]: args=p.parse_args(''.split())
In [55]: args
Out[55]: Namespace(input_file=None)
In [56]: name=os.path.join(os.getcwd(),args.input_file)
...
AttributeError: 'NoneType' object has no attribute 'startswith'
使用现有文件名:
In [57]: args=p.parse_args('-i test'.split())
In [58]: args
Out[58]: Namespace(input_file='test')
In [59]: name=os.path.join(os.getcwd(),args.input_file)
In [60]: name
Out[60]: '/home/paul/test' # valid file name
In [61]: os.path.exists(name)
Out[61]: True # it exists on my system
名字随机选择
In [62]: args=p.parse_args('-i random'.split())
In [63]: name=os.path.join(os.getcwd(),args.input_file)
In [65]: os.path.exists(name)
Out[65]: False # it doesn't exist
重复 exists
测试表明没有创建文件。
我见过类似的问题,但没有适用于这种情况的解决方案。我有一个 argparser 函数:
def IO_fileParse():
parser = argparse.ArgumentParser();
parser.add_argument("-i", dest="input_file", help="no file with this name")
args = parser.parse_args();
input_file = os.path.join(os.getcwd(),args.input_file)
if not os.path.exists(input_file):
print(" ------------------------------------------------");
print("Error: -input_file Cannot find input file with that name.");
print(" ------------------------------------------------");
parser.print_help();
sys.exit();
我想说"if you can't find a file with the -i name in the directory, print the help and exit the script."
实际发生的情况是,这似乎用输入文件的名称创建了一个空文件?
如何将上面的代码更改为 "if -i file does not exist, tell user this file does not exist"(并且不要使用不存在的文件名创建一个空文件)。
我也试过 os.path.isfile 和 os.path.exists,但都没有给我想要的错误?...我也试过 args.input_file [=] =12=]
- 去掉分号。他们为什么在那里?
您没有正确访问变量。
您将其存储在名为 -i 的变量中。尝试调用它 -input_file,删除 dest,然后通过 args.input_file
访问它
os.getcwd(),会给你工作目录,而不是当前路径。获取当前路径
os.path.dirname(os.path.abspath("__file__"))
我不知道你说得对不对,但是我建议的结构用法是
python test.py -input_file filename
编辑:您可以通过 parser.add_argument("-i", "--input_name")
这段代码对我有用
def IO_fileParse():
parser = argparse.ArgumentParser()
parser.add_argument("-input_file", help="no file with this name")
args = parser.parse_args()
input_file = os.path.join(os.getcwd(),args.input_file)
print(input_file)
if not os.path.exists(input_file):
print(" ------------------------------------------------")
print("Error: -input_file Cannot find input file with that name.")
print(" ------------------------------------------------")
parser.print_help()
sys.exit()
IO_fileParse()
除了小的调整,我没有发现你的脚本有问题。这是我的测试:
In [52]: p=argparse.ArgumentParser()
In [53]: p.add_argument('-i','--input-file')
...
没有标记选项,默认为None
,路径连接不起作用。应该给它一个有效的默认值或使用 if args.input_file is not None:
测试。
In [54]: args=p.parse_args(''.split())
In [55]: args
Out[55]: Namespace(input_file=None)
In [56]: name=os.path.join(os.getcwd(),args.input_file)
...
AttributeError: 'NoneType' object has no attribute 'startswith'
使用现有文件名:
In [57]: args=p.parse_args('-i test'.split())
In [58]: args
Out[58]: Namespace(input_file='test')
In [59]: name=os.path.join(os.getcwd(),args.input_file)
In [60]: name
Out[60]: '/home/paul/test' # valid file name
In [61]: os.path.exists(name)
Out[61]: True # it exists on my system
名字随机选择
In [62]: args=p.parse_args('-i random'.split())
In [63]: name=os.path.join(os.getcwd(),args.input_file)
In [65]: os.path.exists(name)
Out[65]: False # it doesn't exist
重复 exists
测试表明没有创建文件。