Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subprocess output to stdout

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.

like image 907
therealtypon Avatar asked May 19 '11 17:05

therealtypon


People also ask

How do I print subprocess output in Python?

To capture the output of the subprocess. run method, use an additional argument named “capture_output=True”. You can individually access stdout and stderr values by using “output. stdout” and “output.

How do you find the output of a subprocess?

popen. To run a process and read all of its output, set the stdout value to PIPE and call communicate(). The above script will wait for the process to complete and then it will display the output.

What does Popen return Python?

Python method popen() opens a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'.


1 Answers

Simply don't send the output to a pipe:

proc = subprocess.Popen (command_args, shell=False)
proc.communicate()
like image 104
Sven Marnach Avatar answered Oct 12 '22 19:10

Sven Marnach