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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import subprocess def run_cmd(args_list): print('Running system command: {0}'.format(' '.join(args_list))) proc = subprocess.Popen(args_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (output, errors) = proc.communicate() if proc.returncode: raise RuntimeError( 'Error running command: %s. Return code: %d, Error: %s' % ( ' '.join(args_list), proc.returncode, errors)) return (output, errors) output, errors = run_cmd(['ls', '-al']) print "run ls -al in python" print output # this will print out all the files under current folder output, errors = run_cmd(['sh', 'test.sh']) print "run sh test.sh command in python" print output # this will execute the test.sh and print the ouput output, errors = run_cmd(['pwd']) print "run pwd command in python" print output # this will print the current directory name |
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.











