Python论坛  - 讨论区

标题:[python-chinese] 如何将 print 的输出保存在一个文件中,同时还要显示在屏幕上?

2007年07月05日 星期四 14:51

Alex Cheng chengxin1985在gmail.com
星期四 七月 5 14:51:26 HKT 2007

¸ß£¬ÊµÔÚÊǸß

ÔÚ07-6-27£¬lai yonghao <lanphaday在gmail.com> дµÀ£º
>
> ÏñÕâÑù£¬¾Í¿ÉÒÔÁË¡£
>
> import sys
>
> class Log(object):
>  def __init__(self, *args):
>   self.f = file(*args)
>   sys.stdout = self
>
>  def write(self, data):
>   self.f.write(data)
>   sys.__stdout__.write(data)
>
> if __name__ == '__main__':
>  log = Log('C:\log.txt', 'w+')
>  print 'I love you!'
>  print 'hello, world!'
>
>
>
> ÔÚ07-6-27£¬Leo Jay <python.leojay在gmail.com> дµÀ£º
> >
> > On 6/27/07, Kevin anew <kevinanew在gmail.com> wrote:
> > > ÈçºÎ½« print µÄÊä³ö±£´æÔÚÒ»¸öÎļþÖУ¬Í¬Ê±»¹ÒªÏÔʾÔÚÆÁÄ»ÉÏ£¿
> > >
> > > ×îºÃ²»ÒªÓú¯ÊýÌæ»» print Óï¾ä£¬³ÌÐòÀïÒѾ­ÓкܶàÁË£¬È«²¿¸ü»»Ì«ÀÛÁË£¬¶øÇÒ»¹ÈÝÒ׳ö´í¡£
> > >
> >
> > python yourscript.py | tee -a output.txt
> >
> > --
> > Best Regards,
> > Leo Jay
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
powered by python
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070705/d1efd1a4/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月05日 星期四 18:41

Marco marco在waven.com
星期四 七月 5 18:41:24 HKT 2007

用logging模块啊, 调试起来比print好多了

On 7/5/07, Alex Cheng <chengxin1985在gmail.com> wrote:
> 高,实在是高
>
> 在07-6-27,lai yonghao <lanphaday在gmail.com> 写道:
> >
> > 像这样,就可以了。
> >
> > import sys
> >
> > class Log(object):
> >  def __init__(self, *args):
> >   self.f = file(*args)
> >   sys.stdout = self
> >
> >  def write(self, data):
> >   self.f.write(data)
> >   sys.__stdout__.write(data)
> >
> > if __name__ == '__main__':
> >  log = Log('C:\log.txt', 'w+')
> >  print 'I love you!'
> >  print 'hello, world!'
> >
> >
> >
> > 在07-6-27,Leo Jay <python.leojay在gmail.com> 写道:
> > >
> > > On 6/27/07, Kevin anew <kevinanew在gmail.com> wrote:
> > > > 如何将 print 的输出保存在一个文件中,同时还要显示在屏幕上?
> > > >
> > > > 最好不要用函数替换 print 语句,程序里已经有很多了,全部更换太累了,而且还容易出错。
> > > >
> > >
> > > python yourscript.py | tee -a output.txt
> > >
> > > --
> > > Best Regards,
> > > Leo Jay
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
>
>
>
> --
> powered by python
>


-- 
LinuX Power

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号