Printing the output of a subprocess while saving the result is not a new problem, and has been answered many times before e.g.: https://mcmap.net/q/25515/-constantly-print-subprocess-output-while-process-is-running
This does not work for me because I am trying to maintain the shell colours printed. E.g. when one goes systemctl status application
, its prints running in green.
The above-mentioned methods all rely on reading a line one by one from subprocess, but it seems to me by then the colour information is stripped off and lost.
I tried to make an object which tee's off the stdout prints and saves them into a variable:
from subprocess import *
import sys
class Tee():
def __init__(self):
self.content = ''
self.stdout = sys.stdout
sys.stdout = self
def __enter__(self):
return self
def __exit__(self, *args):
pass
def __del__(self):
sys.stdout = self.stdout
def write(self, data):
self.content += data
self.stdout.write(data)
def flush(self):
self.content = ''
with Tee() as tee:
# Saves print to tee.content
print("Hello World")
# This line does not save prints to tee.content
run(['apt-get', 'update'])
# raises an error that tee.fileno is not supported
run(['systemctl', 'status', 'nginx'], stdout=tee)
content = tee.content
print("---------------------")
print(content)
But the problem is subprocess's stdout requires an actual file: https://mcmap.net/q/1158601/-creating-a-custom-sys-stdout-class
Is there anyway to print realtime the output of a subprocess, while maintaining the colours, and store the value to a variable (without going through a temp file)?
termios
does not exist in windows. – Moria