python stdout 重定向
2015-11-24 10:30
495 查看
import sys class Buffer(object): def __init__(self): self.buffer = [] def write(self, *args, **kwargs): self.buffer.append(args) def test(): stdout = sys.stdout sys.stdout = open('hello_stdout', 'wb') print 'test' sys.stdout.close() sys.stdout = stdout stdout = sys.stdout sys.stdout = Buffer() print 'buffer1' print 'buffer2' buff, sys.stdout = sys.stdout, stdout print buff.buffer if __name__ == '__main__': test()
跟 decorator 用在一起会比较有趣
相关文章推荐
- 用 Python 和 OpenCV 检测图片上的条形码
- Python 绘制 误差 箱线图 error boxplot
- python美女图片抓取
- 学习Python(四)
- python之路第五篇之递归(进阶篇:续:经典例子剖析)
- python入门(4)
- Python中time模块与datetime模块在使用中的不同之处
- python-04:接下来做什么
- python-06:爬虫基础知识
- python-07:最简单的爬虫程序
- python-08:理解最简单的爬虫程序
- python-09:第一个实践-爬取糗事百科段子
- python-10:将网页源码爬取下来
- python-12:怎么在爬虫代码中伪装header
- python-13:解决header的问题
- python默认值陷阱
- Python开山篇
- python中动态变量创建
- Python实例(1)- 中文转拼音
- caffe使用python创建lmdb