在子进程中获取命令的输出

getting output of command in subprocess

我看到了一些与此相关的话题,但那里的一切都没有帮助我。

我正在 运行通过 python(使用 2.7)

在 cmd 上为 运行 命令设置一个子进程
p = subprocess.Popen(["start",  "cmd", "/k", command], shell=True)

此命令有效,但我无法捕获命令的输出。

我试过 check_output 或指定 stdout=sp.PIPEstdout=file,但没有成功。

如有任何建议,我们将不胜感激。

谢谢

check_output 应该可以正常工作:

from subprocess import check_output

out = check_output(["echo", "Test"], shell=True)

命令输出:

>>> print out
Test