如何捕获子进程的 stdout 输出? [英] How can I capture the stdout output of a child process?

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

问题描述

我正在尝试用 Python 编写程序,但有人告诉我要运行一个 .exe 文件.当这个 .exe 文件运行时,它会吐出大量数据,我需要将某一行打印到屏幕上.我很确定我需要使用 subprocess.popen 或类似的东西,但我是 subprocess 的新手并且不知道.任何人都有一种简单的方法可以让我完成这项工作?

I'm trying to write a program in Python and I'm told to run an .exe file. When this .exe file is run it spits out a lot of data and I need a certain line printed out to the screen. I'm pretty sure I need to use subprocess.popen or something similar but I'm new to subprocess and have no clue. Anyone have an easy way for me to get this done?

推荐答案

是这样的:

import subprocess
process = subprocess.Popen(["yourcommand"], stdout=subprocess.PIPE)
result = process.communicate()[0]

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

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