Running External Commands

Overview

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.

An Example

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.

Posted: May 25th, 2017 at 5:07 am
Last Updated on: August 17th, 2017 at 4:04 am