Python subprocess output to stdout
Asked Answered
A

2

22

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.

Argillaceous answered 19/5, 2011 at 17:15 Comment(3)
Nevermind guys, I think I found the solution: Basically you have 3 options: Use threading to read in another thread without blocking the main thread. select on stdout, stderr instead of communicate. This way you can read just when data is available and avoid blocking. Let a library solve this, twisted is a obvious choice.Argillaceous
If you find another answer, like #4586192 , please link to it instead of duplicating the answer in comment to your own question.Bookrack
Sorry about that. I thought the point in the comment was the distilled essence of stackoverflow.com/questions/4585692… I'll keep that in mind next timeArgillaceous
B
22

Simply don't send the output to a pipe:

proc = subprocess.Popen (command_args, shell=False)
proc.communicate()
Banns answered 19/5, 2011 at 17:17 Comment(4)
@josh: Yes, that's expected, and the reason why the code does not store the result in a variable. Since we don't send the output to a pipe, the subprocess will inherit stdout from the current process, meaning everything will be printed to the terminal directly.Banns
Sorry, you're right -- I didn't read the question carefully enough, I assumed he wanted to do what I want to do, which is to get both... real time output to stdout, and then capturing the output afterwardsThorsten
@josh: If it's ok for you to block while waiting for the output, and processinf the output doesn't take long, you can simply pass the argument bufsize=1 to Popen() for line buffering, read the output line by line, print it as soon as you get it and process it.Banns
nice trick, i can see the lines printing on the cmd, how can i store that into a variable that i can use later?Ranch
I
0

This work for me:

If need execute command in powershell :

import subprocess

S_V = subprocess.Popen(["powershell.exe", 'your command'   ], stdout=subprocess.PIPE)
out, err = S_V.communicate()
print (out)
print (err )

Or if need execute command in CMD :

name=subprocess.check_output('whoami')  # replace your command with 'whoami'
name=name.decode("utf-8") 
print (name)

Note: whoami Displays user for the user who is currently logged on to the local system .

Ignoble answered 19/1, 2023 at 16:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.