I have a requirement to run a shell command in Python, and simply print out its output back to the screen, without changing what is written to stdout and stderr, in real time.
I can see that similar questions have been asked before but I can't find where this was ever answered simply.
I have tried things like
import subprocess, sys
command = "some long shell command".split(" ")
proc = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
while True:
out = proc.stdout.read(1)
if out:
sys.stdout.write(str(out))
sys.stdout.flush()
err = proc.stderr.read(1)
if err:
sys.stderr.write(str(err))
sys.stderr.flush()
if proc.poll():
break
This does not work, but it probably helps to understand what I am trying to do.