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 like:
call(['ls'])
You should separate arguments:
call(['bash', 'run.sh'])
call(['ls','-l'])
from subprocess import call
import shlex
call(shlex.split('bash run.sh'))
You want to properly tokenize your command arguments. shlex.split()
will do that for you.
Source: https://docs.python.org/2/library/subprocess.html#popen-constructor
Note shlex.split() can be useful when determining the correct tokenization for args, especially in complex cases:
When you call call()
with a list
, it expects every element of that list to correspond to a command line argument.
In this case it is looking for bash run.sh
as the executable with spaces and everything as a single string.
Try one of these:
call("bash run.sh".split())
call(["bash", "run.sh"])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With