简体   繁体   中英

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.

Simply don't send the output to a pipe:

proc = subprocess.Popen (command_args, shell=False)
proc.communicate()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM