tags: python I often form a list of commands in Python to execute in a batch. There are two choices: write a bash script, or dump the commands to json. If the latter, this is a simple script that runs all commands in a json file. The json file must be a list of lists of strings. Else things will go wrong. ```py import json import subprocess import sys args = sys.argv[1:] def pex(e,what): print(f"Exception {type(e)}:{e} doing {what}") for arg in args: try: with open(arg) as f: cmds = json.load(f) except Exception as e: pex(e,f"Load {arg}") continue for cmd in cmds: try: m = subprocess.run(cmd) except Exception as e: pex(e,f"Run {cmd}") continue if m.returncode > 0: pex(Exception(f"Command returned {m.returncode}"),f"executing {cmd}") continue ``` Note that the `except Exception as e` thing is bad practice for anything of nontrivial complexity. This sort of script is of 'trivial complexity' and hence one can safely get away with such things. It is clear that all this script does is: 1. For each command line argument 1. Try to load as json (else error and continue to next art) 2. For each element in the list, each element being a list of strings, run `subprocess.run` on the list.