【发布时间】:2015-06-15 23:23:05
【问题描述】:
我发现了一些看起来像我的问题,但没有产生我可以使用的解决方案(最接近的是:subprocess output to stdout and to PIPE)
问题:我想使用子进程启动一个需要很长时间的进程。运行命令后,我需要解析 stdout-output 和 stderr-output。
目前我的做法如下:
p = subprocess.Popen( command_list, stdout=subprocess.PIPE,
stderr=subprocess.PIPE )
out, error_msg = p.communicate()
print out + "\n\n" + error_msg
#next comes code in which I check out and error_msg
但是这种方法的缺点是用户在运行时看不到进程的输出。仅在最后打印输出。
有没有一种方法可以在命令运行时打印输出(就像我在没有 stdout/stderr=subprocess.PIPE 的情况下给出的命令一样)并且最后仍然通过 p.communicate 输出?
注意:我目前正在 python 2.5(使用此 python 版本的旧软件版本)上进行开发。
【问题讨论】:
标签: python subprocess