sys.stdout.write('stdout: ' + read)
stdout.append(read)
if fd == p.stderr.fileno():
    read = p.stderr.readline()
sys.stderr.write('stderr: ' + read)
stderr.append(read)
if p.poll() != None:
    break
print('program ended')
print('stdout:', ''.join(stdout))
print('stderr:', ''.join(stderr))
Can you make a python subprocess output stdout and stderr as usual, but also capture the output as a string?