I am working on a Raspberry Pi OS, and I make this Python script to run a command and capture it’s output. It works fine for commands that outputs text in a sequential way. But when I try to run commands that use dynamic updates, like apt-get update
that display a progres percentage, it fails to capture it. Dow you know how can I do to replicate the exact display behavior of the command, like it is shown in a real terminal window ? The thing is that I don’t realy use print(line.strip())
, instead I send the data over a network socket on another computer, and there I display it. On the other computer I know how to print the data in the virtual terminal windows, but what I don’t know is how can I read the control characters(?) and how I interpret them… ? I dont’ need it to work with complex programs with menus or other stuff, just simple commands like that shows some progress like apt-get update
does…
import subprocess
import select
import os
os.environ['PYTHONUNBUFFERED'] = '1'
process = subprocess.Popen(['apt-get', 'update'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
while True:
line = process.stdout.readline()
if not line: break
print(line.strip())
process.wait()