如何使用python脚本调用外部程序并检索输出并返回代码?
查看子进程模块:一个简单的例子如下......
from subprocess import Popen, PIPE process = Popen(["ls", "-la", "."], stdout=PIPE) (output, err) = process.communicate() exit_code = process.wait()
继Ambroz Bizjak先前的评论之后,这是一个对我有用的解决方案:
import shlex from subprocess import Popen, PIPE cmd = "..." process = Popen(shlex.split(cmd), stdout=PIPE) process.communicate() exit_code = process.wait()