python学习之 sys.stdout

  1. 来源:http://blog.csdn.net/wuxiushu/article/details/52358172
    sys.stdout 与 print  
  2. 当我们在 Python 中打印对象调用 print obj 时候,事实上是调用了 sys.stdout.write(obj+'\n')  
  3. print 将你需要的内容打印到了控制台,然后追加了一个换行符  
  4. print 会调用 sys.stdout 的 write 方法  
  5. 以下两行在事实上等价:  
  6. sys.stdout.write('hello'+'\n')   
  7. print 'hello'  
  8. sys.stdin 与 raw_input  
  9. 当我们用 raw_input('Input promption: ') 时,事实上是先把提示信息输出,然后捕获输入  
  10. 以下两组在事实上等价:  
  11. hi=raw_input('hello? ')   
  12.   
  13. print 'hello? '#comma to stay in the same line   
  14. hi=sys.stdin.readline()[:-1# -1 to discard the '\n' in input stream  
  15.    
  16. 从控制台重定向到文件  
  17. 原始的 sys.stdout 指向控制台  
  18. 如果把文件的对象的引用赋给 sys.stdout,那么 print 调用的就是文件对象的 write 方法  
  19. f_handler=open('out.log''w')   
  20. sys.stdout=f_handler   
  21. print 'hello'  
  22. # this hello can't be viewed on concole   
  23. # this hello is in file out.log  
  24. 记住,如果你还想在控制台打印一些东西的话,最好先将原始的控制台对象引用保存下来,向文件中打印之后再恢复 sys.stdout  
  25. __console__=sys.stdout   
  26. # redirection start #   
  27. ...   
  28. # redirection end   
  29. sys.stdout=__console__  
  30.   
  31. 同时重定向到控制台和文件  
  32. 如果我们希望打印的内容一方面输出到控制台,另一方面输出到文件作为日志保存,那么该怎么办?  
  33. 将打印的内容保留在内存中,而不是一打印就将 buffer 释放刷新,那么放到一个字符串区域中会怎样?  
  34. a=''   
  35. sys.stdout=a   
  36. print 'hello'  
  37. OK,上述代码是无法正常运行的  
  38. Traceback (most recent call last): File   
  39. ".\hello.py", line xx, in print 'hello'   
  40. AttributeError: 'str'   
  41. object has no attribute 'write'  
  42. 错误很明显,就是上面强调过的,在尝试调用 sys.stdout.write() 的时候,发现没有 write 方法  
  43. 另外,这里之所以提示 attribute error 而不是找不到函数等等,我猜想是因为 python 将对象/类的函数指针记录作为对象/类的一个属性来对待,只是保留了函数的入口地址  
  44. 既然这样,那么我们必须给重定向到的对象实现一个 write 方法:  
  45. import sys   
  46. class __redirection__:   
  47.   def __init__(self):   
  48.     self.buff=''   
  49.     self.__console__=sys.stdout   
  50.   
  51.   def write(self, output_stream):   
  52.     self.buff+=output_stream   
  53.     
  54.   def to_console(self):   
  55.     sys.stdout=self.__console__   
  56.     print self.buff   
  57.     
  58.   def to_file(self, file_path):  
  59.     f=open(file_path,'w')   
  60.     sys.stdout=f   
  61.     print self.buff   
  62.     f.close()   
  63.   
  64.   def flush(self):   
  65.     self.buff=''   
  66.    
  67.   def reset(self):   
  68.     sys.stdout=self.__console__   
  69.   
  70. if __name__=="__main__":   
  71.   # redirection   
  72.   r_obj=__redirection__()   
  73.   sys.stdout=r_obj  
  74.   
  75.   # get output stream   
  76.   print 'hello'   
  77.   print 'there'   
  78.   
  79.   # redirect to console   
  80.   r_obj.to_console()   
  81.   
  82.   # redirect to file   
  83.   r_obj.to_file('out.log')   
  84.   
  85.   # flush buffer  
  86.   r_obj.flush()   
  87.   
  88.   # reset   
  89.   r_obj.reset()  
  90. 同样的,sys.stderr, sys.stdin 也都可以被重定向到多个地址,举一反三的事情就自己动手实践吧  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值