Python 子进程输出到 stdout
我正在使用 subprocess 模块从 python 运行二进制文件。
为了捕获二进制文件产生的输出,我使用:
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)
它的作用是在进程执行完成后打印进程的输出。无论如何,我可以在程序执行时将此输出打印到标准输出吗?我真的需要看看输出是什么,因为这些程序可以运行很长时间。
感谢您的帮助。
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.
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是不要将输出发送到管道:
Simply don't send the output to a pipe:
这对我有用:
如果需要在
powershell
中执行命令:或者如果需要在
CMD
中执行命令:注意:
whoami
显示用户的用户当前已登录到本地系统。This work for me:
If need execute command in
powershell
:Or if need execute command in
CMD
:Note:
whoami
Displays user for the user who is currently logged on to the local system .