subprocess output to stdout and to PIPE

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()

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.

How can i do that ?

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

I am using Python 3.x on Windows env.

Thanks.


There's no such thing as a pipe that goes to two places. Everything written to a pipe will only be read once. (While it's theoretically possible for your program and the console to have access to the same out pipe, if you succeed in doing so then only some of the data will go to your program, and only the data that doesn't will end up on the console.) To get all the output to your program and to the console, someone will have to read and duplicate the data. On a unix-like system, you might use the "tee" command for this, but you probably don't have that on your Windows machine.

So you will have to write the output to the console as you get it.

In this case, you can probably get away with using readline() in a loop instead of readlines().


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)

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.

链接地址: http://www.djcxy.com/p/77140.html

上一篇: 进程运行时不断打印子进程输出

下一篇: 子过程输出到标准输出和PIPE