How to call a shell script from python code?


Question

How to call a shell script from python code?

1
182
5/3/2018 8:52:48 PM

Accepted Answer

The subprocess module will help you out.

Blatantly trivial example:

>>> import subprocess
>>> subprocess.call(['./test.sh']) # Thanks @Jim Dennis for suggesting the []
0 
>>> 

Where test.sh is a simple shell script and 0 is its return value for this run.

216
10/7/2017 3:15:35 AM

There are some ways using os.popen() (deprecated) or the whole subprocess module, but this approach

import os
os.system(command)

is one of the easiest.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon