List of files (which can contain wildcards) as arguments to Python script

In many python scripts I find myself doing the following:

for maybe_glob in sys.argv[1:]:
    for filename in glob.iglob(maybe_glob):
        print(filename)

      

I have to do this because scripts also need to run on terminals that do not expand wildcards (like windows). Is there a shorter version for this? Is there a way (with argparser, for example) to directly expand wildcards while parsing arguments?

thank

+3


source to share


1 answer


You can avoid the double loop with a chaining iterator, but that hardly seems like an improvement.

for fname in itertools.chain(*map(glob.iglob, sys.argv[1:])):
    print fname

      



But you can wrap it up in a routine:

def allglob(args):
    return itertools.chain.from_iterable(map(glob.iglob, args))

      

+1


source







All Articles