子进程输出到标准输出和管道 [英] subprocess output to stdout and to PIPE

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

问题描述

我正在使用这样的 subprocess 模块:

I am using the subprocess module like that:

ping = subprocess.Popen('fping.exe 192.168.2.3 196.65.58.69', stdout=PIPE)
output = ping.stdout.readlines()

我需要 output 列表以便稍后在程序中处理它,但似乎因为 stdout 被定向到 PIPE 它不输出结果到控制台.我想同时获得控制台输出(在执行时)和 output 列表.

I am need the output list in order to process it later at the program, but it seems since the stdout is directed to PIPE it isn't output the results to the console. I would like to get both the console output (as it being executed) and the output list.

我该怎么做?

我进行了搜索,并在此处得到了答案,但我无法实施.

I have done a search, and got an answer here, but i am unable to implement it.

我在 Windows 环境中使用 Python 3.x.

I am using Python 3.x on Windows env.

谢谢.

推荐答案

我找到了一种方法,就是这样:

I have found a way to do that here is it:

for line in os.popen("Fping x.x.x.x x.x.x.x -l"):
    ipList.append(line)
    print(line)

这样,我就可以将 Fping 程序的结果放入列表中,并在执行时将其打印到屏幕上,因为 foros 循环.popen 不是等待程序完成,而是总是在程序的每一行循环.

That way, i am able to get the results from the Fping program into the list, and to print it to the screen while it is executing, since the for loop with the os.popen aren't wait for the program to finish, but always looping at every line from the program.

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

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