python redirect stdout to file and console code example
Example 1: redirect python output to file
$ python foo.py > file
Example 2: python write subprocess stdout stderr to file
with open("stdout.txt","wb") as out, open("stderr.txt","wb") as err:
subprocess.Popen("ls",stdout=out,stderr=err)