gpt4 book ai didi

Python 将子进程重定向到文件和控制台

转载 作者:可可西里 更新时间:2023-11-01 10:19:54 34 4
gpt4 key购买 nike

<分区>

我想使用 Python 创建一个子进程并将其输出重定向到文件控制台。

我找到了 this post解释如何打印到控制台和文件,但解决方案在创建子进程时不起作用:

sys.stdout = Logger()
print( "Hello") # printed to console and file
res = subprocess.call(cmd) # command output is printed to console only

这里的行为相同:

with Tee('outfile.log', 'w'):
print( "Hello" )
res = subprocess.call(cmd)

我如何将子进程输出重定向到控制台(对于用户)和文件(对于我从我的代码中检查它)。

注意:我在 Windows 上,所以使用系统的 tee 是不合适的。

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com