Directing script output to a file using subprocess?
From within a python script ("main.py"), I am using the subprocess module to run another script ("sub_script.py").
Here's the code in the "main.py" script that 'runs' "sub_scrip开发者_StackOverflow社区t.py":
subprocess.Popen([sys.executable, "sub_script.py"])
this works fine as long as "sub_script.py" does not have any "print" statements in it.
I now want to channel all the output of "sub_script.py" to an external file ("log.txt").
How do I do it?
subprocess.Popen([sys.executable, "sub_script.py"], stdout=open("log.txt", "a"))
精彩评论