from contextlib import closing
import pexpect
with open('text.txt', 'rb') as logfile:
    with closing(pexpect.spawn(command, logfile=logfile)) as child:
        pass
Python subprocess.check_call - how to direct stdout and stderr to _both_ screen and log file?