开发者

Directing script output to a file using subprocess?

开发者 https://www.devze.com 2023-01-31 17:46 出处:网络
From within a python script (\"main.py\"), I am using the subprocess module to run another script (\"sub_script.py\").

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"))
0

精彩评论

暂无评论...
验证码 换一张
取 消