Skip to content

Instantly share code, notes, and snippets.

@root42
Last active May 17, 2023 09:53
Show Gist options
  • Save root42/7b4f5c6f42de5f18f83a05b5521c4a60 to your computer and use it in GitHub Desktop.
Save root42/7b4f5c6f42de5f18f83a05b5521c4a60 to your computer and use it in GitHub Desktop.
Python exec with file globbing
#!/usr/bin/env python
#
# Executes the given command line by applying file globbing to all
# arguments but the first one. Example:
#
# ./glob_exec.py ls '*.py'
#
# This is useful for shells like Windows cmd which doesn't have native
# file globbing.
#
import glob
import subprocess
import sys
def main():
if len(sys.argv) == 1:
print("Usage: %s <command> <globbed-args> ...\n" % sys.argv[0])
sys.exit(1)
glob_args = [sys.argv[1]]
for arg in sys.argv[2:]:
glob_arg = glob.glob(arg)
if glob_arg is None or glob_arg == []:
glob_args = glob_args + [arg]
else:
glob_args = glob_args + glob_arg
ret = subprocess.call(glob_args)
sys.exit(ret)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment