I would like to call an external script from within a function, for instance:
import subprocess
def call_script(script):
subprocess.call(script)
return answer #retrieving the result is the part I'm struggling with
call_script("/user/bin/calc_delta.py")
The script calc_delta.py simply prints the result when it is done. How can I assign the result of that script to a variable? Thank you.
Instead of using
subprocess.callyou should usePopenand callcommunicateon it.That will allow you to read
stdoutandstderr. You can also input data withstdin.Example from the docs http://docs.python.org/library/subprocess.html#replacing-bin-sh-shell-backquote: