0

Curently I am using subporcess library on widows to execute comand in cmd. My problem is that I would like to display the output of cmd in the real time. I am able to display output after comand exececute her job. Is it possible to do display the output in real time?

My code looks like this:

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'))
Mihael Waschl
  • 330
  • 1
  • 6
  • 19

1 Answers1

0

Would this be helpful to You?

  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

You might find helpful this link.

Gaming.ingrs
  • 271
  • 1
  • 13