Python 子进程输出到标准输出 [英] Python subprocess output to stdout

查看:49
本文介绍了Python 子进程输出到标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 subprocess 模块从 python 运行二进制文件.

I am using the subprocess module to run binaries from python.

为了捕获二进制文件产生的输出,我使用:

To capture the output produced by the binary, I am using:

proc = subprocess.Popen (command_args, shell=False, stdout=subprocess.PIPE)
out = proc.communicate()[0]
#print the output of the child process to stdout
print (out)

它的作用是在进程执行完毕后打印进程的输出.无论如何,我可以在程序执行时将此输出打印到标准输出吗?我真的需要看看输出是什么,因为这些程序可以运行很长时间.

What this does is print the output of the process AFTER it has finished executing. Is there anyway I can print this output to stdout WHILE the program is executing? I really need to see what the output is because these programs can run for a long time.

感谢您的帮助.

推荐答案

不要将输出发送到管道:

Simply don't send the output to a pipe:

proc = subprocess.Popen (command_args, shell=False)
proc.communicate()

这篇关于Python 子进程输出到标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆