0

What's the correct way to handle Unix style wildcard arguments using optparse in Python? I have:

myscript.py:

from optparse import OptionParser
parser = OptionParser()
parser.add_option("--input", dest="input", default=None, nargs=1)
parser.add_option("--outdir", dest="outdir", default=None, nargs=1)
(options, args) = parser.parse_args()

I want to be able to do:

myscript.py --input *.txt --outdir mydir/

I don't want to necessarily read the contents of all the files matching *.txt. I want myscript.py to access their filenames, because some scripts just pass on the filenames to other programs without needing to open/read the files. How can I get an iterator that returns the filenames, while still allowing other arguments like --outdir to be passed after the wildcard friendly option (in this case --input)? thanks.

4
  • 1
    Do you take into account that UNIX shells will expand this glob themselves? Commented Feb 24, 2013 at 0:06
  • @wRAR if they expand it, does it mean that I get passed in a list of filenames? If *.txt was always translated to a.txt,b.txt,... etc there would be no problem. But I don't think that this is what happens Commented Feb 24, 2013 at 0:07
  • Yes, it will be expanded to a space-separated list of filenames. Commented Feb 24, 2013 at 0:21
  • Note: Using optparse is discouraged since python version 2.7. The optparse module is deprecated and will not be developed further; development will continue with the argparse module. See PEP 0389 for more info. Commented Apr 3, 2013 at 23:19

1 Answer 1

2

Unix shells will expand *.txt into separate arguments before they are passed to your program; Windows' command interpreter will not.

Assuming you're using an environment where they aren't expanded first -- that is, invoking python prog.py '*.txt', for instance, you can use glob.glob() to do the expansion yourself.

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

1 Comment

It's unusual to require single quotes though.. I want my program to work like other unix utilities. Like wc *.txt. Also parsing it with glob seems complicated - what if I get passed in a filename that has * in it? How can I tell if it's a glob to be expanded or part of the filename?

Your Answer

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