Run shell script from python

I am trying to run a shell script from a python script using the following:

from subprocess import call call(['bash run.sh']) 

This gives me an error, but I can successfully run other commands, for example:

 call(['ls']) 
+6
source share
3 answers

You must separate the arguments:

 call(['bash', 'run.sh']) call(['ls','-l']) 
+9
source
 from subprocess import call import shlex call(shlex.split('bash run.sh')) 

You want to properly marc your team arguments. shlex.split() will do it for you.

Source: https://docs.python.org/2/library/subprocess.html#popen-constructor

Note. shlex.split () can be useful in determining the correctness of tokenization for args, especially in complex cases:

+4
source

When you call call() with list , it expects each item in this list to match a command line argument. In this case, it searches for bash run.sh as an executable file with spaces and just as one line.

Try one of the following:

 call("bash run.sh".split()) call(["bash", "run.sh"]) 
+3
source

All Articles