7

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'])
1

3 Answers 3

13

You should separate arguments:

call(['bash', 'run.sh'])
call(['ls','-l'])
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to run .sh scripts in Python on Windows?
5
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:

Comments

3

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"])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.