0

I am trying to run the following code in Python which calls a Bash script file and saves its output to a variable. I am trying to use subprocess.check_output but it raises an error to the effect of "No such file or directory". subprocess.call does not work either. Here is a bit of my code.

answer = subprocess.check_output(['/directory/bashfile.bash -c /directory/file -i input -o output'])
print answer

-c -i and -o are just arguments to the script bashfile.

Cong Ma
  • 10,692
  • 3
  • 31
  • 47
sshays68
  • 57
  • 6

1 Answers1

2

The problem is that you are passing the entire command string rather than splitting them into args. You either need to pass it as a shell command:

answer = subprocess.check_output('/directory/bashfile.bash -c /directory/file -i input -o output', 
                                 shell=True)
print answer

Or you need to tokenize it yourself:

answer = subprocess.check_output(['/directory/bashfile.bash', 
                                  '-c', '/directory/file',
                                  '-i', 'input',
                                  '-o', 'output'])
print answer

For more information about subprocess refer to the docs (python 3 version here)! Specifically you will want to read the section about "Frequently used arguments"

Chad S.
  • 6,252
  • 15
  • 25