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'])
asked Aug 18, 2015 at 23:18
1

3 Answers 3

13

You should separate arguments:

call(['bash', 'run.sh'])
call(['ls','-l'])
answered Aug 18, 2015 at 23:22
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:

answered Aug 18, 2015 at 23:20

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"])
answered Aug 18, 2015 at 23:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.