import subprocess import sys with open('test.log', 'w') as f: process = subprocess.Popen(your_command, stdout=subprocess.PIPE) # for python2, use '' for c in iter(lambda: process.stdout.read(1), ''): sys.stdout.write(c) f.write(c) # for python3, use b'' for c in iter(lambda: process.stdout.read(1), b''): sys.stdout.write(c) f.write(c)
or
import subprocess import sys with open('test.log', 'w') as f: process = subprocess.Popen(your_command, stdout=subprocess.PIPE) # for python2, use '' for line in iter(process.stdout.readline, ''): sys.stdout.write(c) f.write(c) # for python3, use b'' for line in iter(process.stdout.readline, b''): sys.stdout.write(c) f.write(c)
live output from subprocess command
stack overflow
python real time - live output from subprocess command
CODE Q&A Solved
Realtime output from a shell command in Python
zaiste blog