输出到控制台和多个文件 [英] output to console and to multiple files

查看:106
本文介绍了输出到控制台和多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我在谷歌和谷歌集团搜索过,但没有找到任何

解决方案来解决我的问题。


我正在寻找一种方法将stdout / stderr(从子进程或

spawn)输出到屏幕和至少两个不同的文件。


例如。

stdout / stderr -screen

stdout -log.out

stderr -log.err


如果可能的话

stdout / stderr -screen和log.txt


来自stdout / stderr的3个文件/>

Hello,

I searched on Google and in this Google Group, but did not find any
solution to my problem.

I''m looking for a way to output stdout/stderr (from a subprocess or
spawn) to screen and to at least two different files.

eg.
stdout/stderr -screen
stdout -log.out
stderr -log.err

and if possible
stdout/stderr -screen and log.txt

3 files from stdout/stderr

推荐答案

我环顾四周,我也找不到任何东西。我将

留意这个帖子,看看是否有人发布了更标准的

解决方案。与此同时,我将提供一个潜在的

解决方案。鸭子打字是你的朋友。如果您只使用文件的写入

方法,那么实现假文件

对象可以非常简单地执行您想要的操作。

I took a look around and I couldn''t find anything either. I will be
keeping an eye on this thread to see if someone posts a more standard
solution. In the mean time though, I will offer up a potential
solution. Duck typing is your friend. If you are only using the write
method of your files, it can be pretty simple to implement a fake file
object to do what you want.

展开 | 选择 | Wrap | 行号


是这样的吗?


类作家(对象):


def __init __(自我,*作家):

self.writers =作家


def写(自我,字符串) :

for self.writers:

w.write(string)


def flush(self):

w.flush():

导入系统

logfile = open(''log.txt'',''w'')

sys.stdout =作家(aya.stdout,文件(''log.out'',''w''),日志文件)

sys.stderr =作家(aya.stdout,file( ''log.err'',''w''),logfile)

like this?

class Writers (object):

def __init__(self, *writers):
self.writers = writers

def write(self, string):
for w in self.writers:
w.write(string)

def flush(self):
for w in self.writers:
w.flush():

import sys

logfile = open(''log.txt'', ''w'')
sys.stdout = Writers(aya.stdout, file(''log.out'', ''w''), logfile)
sys.stderr = Writers(aya.stdout, file(''log.err'', ''w''), logfile)


En Wed,2007年2月14日19:28:34 -0300, na**********@gmail.com

< na ********** @gmail.comescribió:
En Wed, 14 Feb 2007 19:28:34 -0300, na**********@gmail.com
<na**********@gmail.comescribió:

我正在寻找输出stdout / stderr的方法(来自一个子进程或

spawn)来筛选和至少两个不同的文件。
I''m looking for a way to output stdout/stderr (from a subprocess or
spawn) to screen and to at least two different files.



查看tee命令。如果您控制子流程,并且在Python中编写了

,那么使用Python配方会更容易,也许您可​​以更好地控制


但如果你不能修改子过程,你将不得不使用发球台。


-

Gabriel Genellina


这篇关于输出到控制台和多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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