开发者

Python subprocess output to stdout

开发者 https://www.devze.com 2023-03-06 22:02 出处:网络
I am using the subprocess module to run开发者_如何学Python binaries from python. To capture the output produced by the binary, I am using:

I am using the subprocess module to run开发者_如何学Python 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 .

0

精彩评论

暂无评论...
验证码 换一张
取 消