将stdout重定向到文件以及屏幕 [英] redirecting stdout to a file as well as screen

查看:401
本文介绍了将stdout重定向到文件以及屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能从程序内部将stdout和

stderr打印到文件和终端(照例)。

How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).

推荐答案

4月12日上午8:14,SamG < mad.vi ... @ gmail.comwrote:
On Apr 12, 8:14 am, "SamG" <mad.vi...@gmail.comwrote:

我怎样才能从程序内部获得stdout和

stderr既可以打印到文件也可以打印到终端(像往常一样)。
How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).



一种方法是创建一个自定义类,它具有相同的方法

作为文件类型,并保存文件列表 - 喜欢写对象。

例如

class multicaster(object):

def __init __(self,filelist):< br $>
self.filelist = filelist


def write(self,str):

for self in files.n / br / >
f.write(str)

def writelines(self,str_list):

#etc


然后将stdout和stderr分配给这些

对象之一的新实例:


mc = multicaster([sys.stdout,sys.stderr,log_file])

sys.stdout = mc

sys.stderr = mc


HTH

One way would be to create a custom class which has the same methods
as the file type, and held a list of file-like objects to write to.
e.g.

class multicaster(object):
def __init__(self, filelist):
self.filelist = filelist

def write(self, str):
for f in self.filelist:
f.write(str)
def writelines(self, str_list):
#etc

Then assign stdout and stderr to a new instance of one of these
objects:

mc = multicaster([sys.stdout, sys.stderr, log_file])
sys.stdout = mc
sys.stderr = mc

HTH

En Thu,2007年4月12日04:14:32 -0300,SamG< ma ******* @gmail.comescribió:
En Thu, 12 Apr 2007 04:14:32 -0300, SamG <ma*******@gmail.comescribió:

我怎样才能从程序内部将stdout和

stderr打印到文件中,因为我们终端(像往常一样)。
How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).



一个非常小的例子:


import sys

class Tee(文件):

others =()


def写(自我,数据):

file.write(self,data )

for f in others:

f.write(data)


tee = Tee(r" c:\ temp\output.log"," wt")

tee.others = [sys.stdout,sys.stderr]

sys.stdout = sys.stderr = tee


print dir(sys)

sys.foo #error


-

Gabriel Genellina

A very minimal example:

import sys

class Tee(file):
others = ()

def write(self, data):
file.write(self, data)
for f in others:
f.write(data)

tee = Tee(r"c:\temp\output.log","wt")
tee.others = [sys.stdout, sys.stderr]
sys.stdout = sys.stderr = tee

print dir(sys)
sys.foo # error

--
Gabriel Genellina


4月12日下午1:00,Gabriel Genellina < gagsl -... @ yahoo.com.ar>

写道:
On Apr 12, 1:00 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:

En Thu,2007年4月12日04:14: 32 -0300,SamG< mad.vi ... @gmail.comescribió:
En Thu, 12 Apr 2007 04:14:32 -0300, SamG <mad.vi...@gmail.comescribió:

我怎样才能从程序内部获得stdout和

stderr既可以打印到文件也可以打印到终端(像往常一样)。
How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).



一个非常小的例子:


import sys

class Tee(文件):

others =()


def写(自我,数据):

file.write(self,data )

for f in others:

f.write(data)


tee = Tee(r" c:\ temp\output.log"," wt")

tee.others = [sys.stdout,sys.stderr]

sys.stdout = sys.stderr = tee


print dir(sys)

sys.foo #error


-

Gabriel Genellina


A very minimal example:

import sys

class Tee(file):
others = ()

def write(self, data):
file.write(self, data)
for f in others:
f.write(data)

tee = Tee(r"c:\temp\output.log","wt")
tee.others = [sys.stdout, sys.stderr]
sys.stdout = sys.stderr = tee

print dir(sys)
sys.foo # error

--
Gabriel Genellina



这只创建一个out.log文件,所有stdout和stderr

都会记录在那里。 />

This is only creating an out.log file and all the stdout and stderr
are logged there.


这篇关于将stdout重定向到文件以及屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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