使用子进程将脚本输出定向到文件?

发布于 2024-10-09 02:31:44 字数 316 浏览 0 评论 0原文

在 python 脚本(“main.py”)中,我使用 subprocess 模块来运行另一个脚本(“sub_script.py”)。

这是“main.py”脚本中“运行”“sub_script.py”的代码:

subprocess.Popen([sys.executable, "sub_script.py"])

只要“sub_script.py”中没有任何“print”语句,它就可以正常工作。

我现在想将“sub_script.py”的所有输出传输到外部文件(“log.txt”)。

我该怎么做?

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_script.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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回首观望 2024-10-16 02:31:44
subprocess.Popen([sys.executable, "sub_script.py"], stdout=open("log.txt", "a"))
subprocess.Popen([sys.executable, "sub_script.py"], stdout=open("log.txt", "a"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文