Python Forum

Full Version: [SOLVED] Good way to handle input args?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

My script could be called with either discrete filenames or a glob:

#myscript.py file1.txt file2.txt
#myscript.py *.txt

for item in sys.argv[1:]:
for file in glob.glob(sys.argv[1]):
Before I hack something, do you know of a good way to handle both cases?

Thank you.
Filenames glob to themselves. If you want to accept globs, I would just pass them all through glob. If one is just a filename, you'll get that filename back. I'd also use itertools to put it into one single list.

import itertools
import glob
filenames = list(itertools.chain.from_iterable([glob.glob(x) for x in sys.argv[1:]]))
Thank you.