我想从 python 运行外部程序,将输出(大量文本)重定向到日志文件并等待该程序完成。我知道我可以通过 bash 做到这一点:
#! /bin/bash
my_external_program > log_file 2>&1
echo "done"
但我怎样才能用 python 做同样的事情呢?请注意,使用 bash 命令,我可以在程序运行时检查 log_file。我也想要 python 中的这个属性。
See the 子流程 https://docs.python.org/3/library/subprocess.html module.
For example https://docs.python.org/3/library/subprocess.html#subprocess.run:
with open("log_file", "w") as log_file:
subprocess.run(["my_external_program"], stdout=log_file, stderr=log_file)
print("done")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)