nlp
nlp

Reputation: 119

How to pass multiple arguments within subprocess.check_call()?

How can I pass multiple arguments within my subprocess call in python while running my shell script?

import subprocess

subprocess.check_call('./test_bash.sh '+arg1 +arg2, shell=True)

This prints out the arg1 and arg2 concatenated as one argument. I would need to pass 3 arguments to my shell script.

Upvotes: 1

Views: 646

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140266

of course it concatenates because you're not inserting a space between them. Quickfix would be (with format, and can fail if some arguments contain spaces)

 subprocess.check_call('./test_bash.sh {} {}'.format(arg1,arg2), shell=True)

can you try (more robust, no need to quote spaces, automatic command line generation):

check_call(['./test_bash.sh',arg1,arg2],shell=True)`

(not sure it works on all systems because of shell=True and argument list used together)

or drop shell=True and call the shell explicitly (may fail because shebang is not considered, unlike with shell=True, but it's worth giving up shell=True, liable to code injection among other issues):

check_call(['sh','-c','./test_bash.sh',arg1,arg2])

Upvotes: 3

Related Questions