实时显示子过程输出

问题描述 投票:0回答:1

我肯定在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'))
python subprocess
1个回答
0
投票

这对你有帮助吗?

  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

© www.soinside.com 2019 - 2024. All rights reserved.