Python Forum
[SOLVED] Good way to handle input args? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [SOLVED] Good way to handle input args? (/thread-33701.html)



[SOLVED] Good way to handle input args? - Winfried - May-18-2021

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.


RE: Good way to handle input args? - bowlofred - May-18-2021

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:]]))



RE: Good way to handle input args? - Winfried - May-18-2021

Thank you.