Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple files for one argument in argparse Python 2.7

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:

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

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_argument to:

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

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

like image 938
O.rka Avatar asked Nov 04 '14 03:11

O.rka


People also ask

How do you pass arguments to Argparse?

After importing the library, argparse. ArgumentParser() initializes the parser so that you can start to add custom arguments. To add your arguments, use parser. add_argument() .

What is Argparse ArgumentParser ()?

The argparse module provides a convenient interface to handle command-line arguments. It displays the generic usage of the program, help, and errors. The parse_args() function of the ArgumentParser class parses arguments and adds value as an attribute dest of the object.

What is Store_true in Python?

The store_true option automatically creates a default value of False. Likewise, store_false will default to True when the command-line argument is not present. The source for this behavior is succinct and clear: http://hg.python.org/cpython/file/2.7/Lib/argparse.py#l861.


1 Answers

If your goal is to read one or more readable 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.

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.

Alternatively, if your goal is to read zero or more readable 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.

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

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

like image 133
mcho421 Avatar answered Sep 21 '22 17:09

mcho421