6

I am new to Python and barely know about lists and tuples. I have a program to execute which takes several values as input argument. Below is the list of input args

parser = argparse.ArgumentParser()
parser.add_argument("server")
parser.add_argument("outdir")
parser.add_argument("dir_remove", help="Directory prefix to remove")
parser.add_argument("dir_prefix", help="Directory prefix to prefix")
parser.add_argument("infile", default=[], action="append")
options = parser.parse_args()

The program works fine with the following command

python prod2dev.py mysrv results D:\Automations D:\MyProduction Automation_PP_CVM.xml

But looking at the code, it seems like the code can accept multiple file names for argument "infile". I have tried following to pass multiple file names but none worked.

python prod2dev.py mysrv results D:\Automations D:\MyProduction "Automation_PP_CVM.xml, Automation_PT_CVM.xml"

python prod2dev.py mysrv results D:\Automations D:\MyProduction ["Automation_PP_CVM.xml", "Automation_PT_CVM.xml"]

python prod2dev.py mysrv results D:\Automations D:\MyProduction ['Automation_PP_CVM.xml', 'Automation_PT_CVM.xml']

python prod2dev.py mysrv results D:\Automations D:\MyProduction ['"Automation_PP_CVM.xml"', '"Automation_PT_CVM.xml"']

The code below is apparently traversing the list

infile = windowsSucksExpandWildcards(options.infile)
 for filename in infile:
    print(filename)
    outfilename = os.path.join(options.outdir, os.path.split(filename)[1])
    if os.path.exists(outfilename):
        raise ValueError("output file exists: {}".format(outfilename))

    with open(filename, "rb") as f:
        root = lxml.etree.parse(f)
    if not isEnabled(root):
        print("Disabled. Skipping.")
        continue
    elif not hasEnabledTriggers(root):
        print("Has no triggers")
        continue
...
...
...
def windowsSucksExpandWildcards(infile):
    result = []
    for f in infile:
        tmp = glob.glob(f)
        if bool(tmp):
            result.extend(tmp)
        else:
            result.append(f)
    return result

Please guide on how to pass multiple filenames (strings) to a single argument "infile" which is apparently a list.

I'm running Python 3.5.1 |Anaconda 4.0.0 (32-bit)

2 Answers 2

8

You pass the nargs argument, not action="append":

parser.add_argument("infile", default=[], nargs='*')

* means zero or more, just like in regular expressions. You can also use + if you require at least one. Since you have a default, I am assuming that the user is not required to pass any.

Sign up to request clarification or add additional context in comments.

2 Comments

And what's the best way to call the program?
@Ali: Just like your first try, but without the quotation marks and without the commas. The quotation marks are used to make something one argument that would be more than one. In this case, they should be separate arguments. The commas are interpreted as commas, not delimiters. The space is the delimiter.
0

Your code, from what all you posted, looks solid.

The problem is with the snippet you posted thats supposed to traverse the list. The way your program is setup you cant use infile as a variable

All you need to do to fix it is switch infile with options.infile

Specifically:

 for filename in options.infile:
    print(filename)

The reason for this is all your arguments are stored in the options "Namespace"-type variable

1 Comment

My bad. Wanted to hide the complexity. Please see my edit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.