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.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
therealtypon
  • 455
  • 2
  • 5
  • 12
  • 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.` – therealtypon May 19 '11 at 17:19
  • 5
    If you find another answer, like http://stackoverflow.com/questions/4585692/python-nonblocking-subprocess-check-stdout/4585898#4585898 , please link to it instead of duplicating the answer in comment to your own question. – phooji May 19 '11 at 17:23
  • Sorry about that. I thought the point in the comment was the distilled essence of https://stackoverflow.com/questions/4585692/… I'll keep that in mind next time – therealtypon May 20 '11 at 20:51

2 Answers2

21

Simply don't send the output to a pipe:

proc = subprocess.Popen (command_args, shell=False)
proc.communicate()
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 3
    @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. – Sven Marnach Jul 24 '13 at 19:28
  • 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 afterwards – josh Jul 25 '13 at 15:19
  • 1
    @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. – Sven Marnach Jul 25 '13 at 18:34
  • nice trick, i can see the lines printing on the cmd, how can i store that into a variable that i can use later? – pelos Jan 20 '17 at 21:32
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 .

henrry
  • 486
  • 6
  • 25