+-
python – 将subprocess.Popen的输出输出到文件
我需要使用subprocess.Popen启动一些长时间运行的进程,并希望将每个进程中的stdout和stderr自动通过管道分离日志文件.每个进程将同时运行几分钟,并且我希望在进程运行时将每个进程写入两个日志文件(stdout和stderr).

我是否需要在循环中的每个进程上不断调用p.communicate()以更新每个日志文件,或者是否有某种方法来调用原始Popen命令以便stdout和stderr自动流式传输以打开文件句柄?

最佳答案
每 the docs,

stdin, stdout and stderr specify the
executed programs’ standard input,
standard output and standard error
file handles, respectively. Valid
values are PIPE, an existing file
descriptor (a positive integer), an
existing file object, and None.

所以只需将open-for-writing文件对象作为命名参数stdout =和stderr =传递,你应该没问题!

点击查看更多相关文章

转载注明原文:python – 将subprocess.Popen的输出输出到文件 - 乐贴网