0

I would like to assemble commands based on input to the python script, but I am having trouble with subprocess.check_output recognizing the string. Here is an example.

 str1 = "./program.sh %lf %lf" % (x0, x1)
 sim_flux230 = subprocess.check_output(str1)

It keeps saying there is no such file or directory in reference to str1. How can I get subprocess to recognize the string str is holding rather than literally checking for a file called str1?

Novice C
  • 1,344
  • 2
  • 15
  • 27

2 Answers2

2

Try using shlex and see if it helps:

import shlex

str1 = "./program.sh %lf %lf" % (x0, x1)
sim_flux230 = subprocess.check_output(shlex.split(str1))

Generally speaking, if you do not have shell=True (i.e. subprocess.check_output(str1, shell=True)), then the command needs to be passed in as a list. It's also not safe to use shell=True.

s16h
  • 4,647
  • 1
  • 21
  • 33
1

When you use subprocess with shell=False, you have to pass your arguments as a list. You can only pass them as a string using shell=True. Your code should work if you do this:

cmd = ["./program.sh", x0, x1]
sim_flux230 = subprocess.check_output(cmd)
dano
  • 91,354
  • 19
  • 222
  • 219