Running External Commands
Python gives you the ability to run/call external commands from within python scripts. For example, you may wish to call a Linux shell program from within your python code.
The below example provides a function which can run a sub-process from within Python. It prints output from the sub-process line-by-line to stdout (which is a great feature for seeing the status of the shell program, instead of being dumped with the entire output once the sub-process is finished). The python function returns when the sub-process finishes, returning the sub-processes exit code (normally you would check if it is non-zero, to see if an error occurred).
The above code should also preserve text colouring (ANSI escape codes). It has been designed to work with Python 3.
shlex is used to split up the string into an array of command-line argument strings, so that the user doesn't have to do this themselves.
This work is licensed under a Creative Commons Attribution 4.0 International License .