argparse Python 2.7 中一个参数的多个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26727314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:56:45  来源:igfitidea点击:

Multiple files for one argument in argparse Python 2.7

pythonfileparsingargumentsargparse

提问by O.rka

Trying to make an argument in argparse where one can input several file names that can be read.In this example, i'm just trying to print each of the file objects to make sure it's working correctly but I get the error:

试图在 argparse 中提出一个参数,其中可以输入几个可以读取的文件名。在此示例中,我只是尝试打印每个文件对象以确保其正常工作,但出现错误:

error: unrecognized arguments: f2.txt f3.txt

. How can I get it to recognize all of them?

. 我怎样才能让它识别所有这些?

my command in the terminal to run a program and read multiple files

我在终端中运行程序并读取多个文件的命令

python program.py f1.txt f2.txt f3.txt

Python script

Python脚本

import argparse

def main():
    parser = argparse.ArgumentParser()      
    parser.add_argument('file', nargs='?', type=file)
    args = parser.parse_args()

    for f in args.file:
        print f

if __name__ == '__main__':
    main()

I used nargs='?'b/c I want it to be any number of files that can be used . If I change add_argumentto:

我使用nargs='?'b/c 我希望它是可以使用的任意数量的文件。如果我add_argument改为:

parser.add_argument('file', nargs=3)

then I can print them as strings but I can't get it to work with '?'

然后我可以将它们打印为字符串,但我无法使用 '?'

采纳答案by mcho421

If your goal is to read one or morereadable files, you can try this:

如果你的目标是读取一个或多个可读文件,你可以试试这个:

parser.add_argument('file', type=argparse.FileType('r'), nargs='+')

nargs='+'gathers all command line arguments into a list. There must also be one or more arguments or an error message will be generated.

nargs='+'将所有命令行参数收集到一个列表中。还必须有一个或多个参数,否则将生成错误消息。

type=argparse.FileType('r')tries to open each argument as a file for reading. It will generate an error message if argparse cannot open the file. You can use this for checking whether the argument is a valid and readable file.

type=argparse.FileType('r')尝试将每个参数作为文件打开以供阅读。如果 argparse 无法打开文件,它将生成错误消息。您可以使用它来检查参数是否是有效且可读的文件。

Alternatively, if your goal is to read zero or morereadable files, you can simply replace nargs='+'with nargs='*'. This will give you an empty list if no command line arguments are supplied. Maybe you might want to open stdin if you're not given any files - if so just add default=[sys.stdin]as a parameter to add_argument.

或者,如果您的目标是读取零个或多个可读文件,您可以简单地替换nargs='+'nargs='*'. 如果没有提供命令行参数,这将为您提供一个空列表。如果您没有得到任何文件,也许您可​​能想打开 stdin - 如果是这样,只需将其default=[sys.stdin]作为参数添加到add_argument.

And then to process the files in the list:

然后处理列表中的文件:

args = parser.parse_args()
for f in args.file:
    for line in f:
        # process file...


More about nargs: https://docs.python.org/2/library/argparse.html#nargs

有关 nargs 的更多信息:https://docs.python.org/2/library/argparse.html#nargs

More about type: https://docs.python.org/2/library/argparse.html#type

更多关于类型:https: //docs.python.org/2/library/argparse.html#type

回答by O.rka

Just had to make sure there was at least one argument

只需要确保至少有一个论点

parser.add_argument('file',nargs='*')