Skip to content

Instantly share code, notes, and snippets.

View ekawahyu's full-sized avatar

Eka Susilo ekawahyu

View GitHub Profile
@thenger
thenger / subprocess_stdout_instantly.py
Created April 29, 2020 09:38
Python display subprocess stdout in real time
# Redirect stderr and stdout of a subprocess to the python ones
def exec_cmd_with_output(cmd):
stdoutlines = []
a_process = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while a_process.poll() is None:
line = a_process.stdout.readline()
stdoutlines.append(line)
sys.stdout.write(line.decode(sys.stdout.encoding))