Python Popen:同时写入stdout和日志文件 [英] Python Popen: Write to stdout AND log file simultaneously

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

问题描述

我正在使用Popen调用Shell脚本,该脚本不断将其stdout和stderr写入日志文件.有什么方法可以同时(连续地)将日志文件输出到屏幕上,或者可以使Shell脚本同时写入日志文件和stdout?

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)

推荐答案

您可以使用管道从程序的stdout中读取数据并将其写入所需的所有位置:

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.

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

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