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.
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.
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.
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'.
Simply don't send the output to a pipe:
proc = subprocess.Popen (command_args, shell=False)
proc.communicate()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With