如何与 Popen 同时写入标准输出和日志文件? [英] How to write to stdout AND to log file simultaneously with Popen?

查看:92
本文介绍了如何与 Popen 同时写入标准输出和日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Popen 调用一个 shell 脚本,该脚本不断将其 stdout 和 stderr 写入日志文件.有没有办法同时连续输出日志文件(到屏幕),或者让 shell 脚本同时写入日志文件和标准输出?

I am using Popen to call a shell script that is continuously writing its stdout and stderr to a log file. Is there any way to simultaneously output the log file continuously (to the screen), or alternatively, make the shell script write to both the log file and stdout at the same time?

我基本上想在 Python 中做这样的事情:

I basically want to do something like this in Python:

cat file 2>&1 | tee -a logfile #"cat file" will be replaced with some script

同样,这会将 stderr/stdout 连接到 tee,然后将其写入 stdout 和我的日志文件.

Again, this pipes stderr/stdout together to tee, which writes it both to stdout and my logfile.

我知道如何在 Python 中将 stdout 和 stderr 写入日志文件.我遇到的问题是如何将这些复制回屏幕:

I know how to write stdout and stderr to a logfile in Python. Where I'm stuck is how to duplicate these back to the screen:

subprocess.Popen("cat file", shell=True, stdout=logfile, stderr=logfile)

当然,我可以做这样的事情,但是有没有办法在没有 tee 和 shell 文件描述符重定向的情况下做到这一点?:

Of course, I could just do something like this, but is there any way to do this without tee and shell file descriptor redirection?:

subprocess.Popen("cat file 2>&1 | tee -a logfile", shell=True)

推荐答案

您可以使用管道从程序的标准输出中读取数据并将其写入您想要的所有位置:

You can use a pipe to read the data from the program's stdout and write it to all the places you want:

import sys
import subprocess

logfile = open('logfile', 'w')
proc=subprocess.Popen(['cat', 'file'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in proc.stdout:
    sys.stdout.write(line)
    logfile.write(line)
proc.wait()

更新

在 python 3 中,universal_newlines 参数控制管道的使用方式.如果 False,管道读取返回 bytes 对象并且可能需要被解码(例如,line.decode('utf-8'))到得到一个字符串.如果 True,python 为你做解码

In python 3, the universal_newlines parameter controls how pipes are used. If False, pipe reads return bytes objects and may need to be decoded (e.g., line.decode('utf-8')) to get a string. If True, python does the decode for you

3.3 版更改:当universal_newlines 为True 时,该类使用编码locale.getpreferredencoding(False) 而不是locale.getpreferredencoding().有关此更改的更多信息,请参阅 io.TextIOWrapper 类.

Changed in version 3.3: When universal_newlines is True, the class uses the encoding locale.getpreferredencoding(False) instead of locale.getpreferredencoding(). See the io.TextIOWrapper class for more information on this change.

这篇关于如何与 Popen 同时写入标准输出和日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆