我肯定在widows上使用subporcess库来执行cmd中的comand。我的问题是我想实时显示cmd的输出。我能够在命令执行她的工作后显示输出。是否可以实时显示输出?
我的代码看起来像这样:
import subprocess
def get_output(command):
process = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
output = process.communicate()[0]
return output.decode('utf-8')
print(get_output('ping 8.8.8.8'))
这对你有帮助吗?
import subprocess
import shlex
def get_output(command):
process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
while True:
output = process.stdout.readline()
if output == '' and process.poll() is not None:
break
if output:
print output.strip()
rc = process.poll()
return rc
你可能会发现有用的this link。