I want to use subprocess to call other py files and get output. The colors they output are the same, How can i distinguish between stdout and stderr in subprocess?
run.py
def run():
for i in range(3):
print('Processing {}.'.format(i))
time.sleep(1)
print(1/0)
run()
main.py
import subprocess
import sys
def byte2str(b):
return str(b, encoding='utf-8')
if __name__ == '__main__':
cmd = [sys.executable, 'temp/run.py']
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while p.poll() is None:
line = p.stdout.readline()
if line:
print(byte2str(line), end='')