This may be a stupid question but I have a Python script that starts a subprocess (also a Python script) and I need that subprocess to return three integers. How do I get those return values from the Python script that starts the subprocess? Do I have to output the integers to stdout and then use the check_output() function?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
The answer is Yes... I want my +15 reputation!! :-D
No, seriously... Once you start dealing with subprocesses, the only way you really have to communicate with them is through files (which is what stdout
, stderr
and such are, in the end)
So what you're gonna have to do is grab the output and check what happened (and maybe the spawned process' exit_code
, which will be zero if everything goes well) Bear in mind that the contents of stdout
(which is what you'll get with the check_output
function) is an string. So in order to get the three items, you'll have to split that string somehow...
For instance:
import subprocess
output = subprocess.check_output(["echo", "1", "2", "3"])
print "Output: %s" % output
int1, int2, int3 = output.split(' ')
print "%s, %s, %s" % (int1, int2, int3)