Run external shell command in Python

There is often a need to call shell command from python directly. In this post, I use examples to show how to run external shell commands in python.

The recommended way to call shell command from python is using the subprocess library. See the following example:

In this post, I use example to show how to run shell command in a python program. However, this method has some problems as the output is buffered into memory.

We need to print out the shell output on the fly if the size is too large. Sometimes, when the shell scripts run for a long time, we may need to examine the output in real time to check the status of the problem. Please refer to this post on how to get the real time output of shell from python