python 的print 是输出到标准输出的。当程序后台执行时。即使通过重定向将其重定向文件。但是还是发现文件没有内容。

经查发现print在重定向文件时是有缓冲的。

解决办法:

一: 通过使用print >>sys.stderr将其重定向到 

      如 print ‘hello world’  就是 print >> sys.stderr, 'hello world'

二、启动时使用python -u 的方式执行脚本

     如 python -u test.py 2>>log.txt 1>&2

refer:http://blog.csdn.net/joeblackzqq/article/details/7220009

posted on 2012-05-11 20:37  @且听风吟@  阅读(475)  评论(0编辑  收藏  举报