I have the following simple program to run a subprocess and tee
its output to both stdout
and some buffer
import subprocess
import sys
import time
import unicodedata
p = subprocess.Popen(
"top",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
stdout_parts = []
while p.poll() is None:
for bytes in iter(p.stdout.readline, b''):
stdout_parts.append(bytes)
str = bytes.decode("utf-8")
sys.stdout.write(str)
for ch in str:
if unicodedata.category(ch)[0]=="C" and ord(ch) != 10:
raise Exception(f"control character! {ord(ch)}")
time.sleep(0.01)
When running some terminal updating program, such as top
or docker pull
, I want to be able to catch its entire output as well, even if it is not immediately readable as such.
Reading around How do commands like top update output without appending in the console? for example, it seems it is achieved by control characters. However, I don't receive any of them when reading lines from the process output streams (stdout/stderr). Or is the technology they use different and I cannot catch it from the subprocess?