本文環境:Python 2.7 python
使用 print obj 而非 print(obj)函數
當咱們在 Python 中打印對象調用 print obj 時候,事實上是調用了 sys.stdout.write(obj+'\n')this
print 將你須要的內容打印到了控制檯,而後追加了一個換行符spa
print 會調用 sys.stdout 的 write 方法指針
如下兩行在事實上等價:日誌
sys.stdout.write('hello'+'\n') print 'hello'
當咱們用 raw_input('Input promption: ') 時,事實上是先把提示信息輸出,而後捕獲輸入code
如下兩組在事實上等價:對象
hi=raw_input('hello? ') print 'hello? ', #comma to stay in the same line hi=sys.stdin.readline()[:-1] # -1 to discard the '\n' in input stream
原始的 sys.stdout 指向控制檯blog
若是把文件的對象的引用賦給 sys.stdout,那麼 print 調用的就是文件對象的 write 方法內存
f_handler=open('out.log', 'w') sys.stdout=f_handler print 'hello' # this hello can't be viewed on concole # this hello is in file out.log
記住,若是你還想在控制檯打印一些東西的話,最好先將原始的控制檯對象引用保存下來,向文件中打印以後再恢復 sys.stdout
__console__=sys.stdout # redirection start # ... # redirection end sys.stdout=__console__
若是咱們但願打印的內容一方面輸出到控制檯,另外一方面輸出到文件做爲日誌保存,那麼該怎麼辦?
將打印的內容保留在內存中,而不是一打印就將 buffer 釋放刷新,那麼放到一個字符串區域中會怎樣?
a='' sys.stdout=a print 'hello'
OK,上述代碼是沒法正常運行的
Traceback (most recent call last): File ".\hello.py", line xx, in <module> print 'hello' AttributeError: 'str' object has no attribute 'write'
錯誤很明顯,就是上面強調過的,在嘗試調用 sys.stdout.write() 的時候,發現沒有 write 方法
另外,這裏之因此提示 attribute error 而不是找不到函數等等,我猜測是由於 python 將對象/類的函數指針記錄做爲對象/類的一個屬性來對待,只是保留了函數的入口地址
既然這樣,那麼咱們必須給重定向到的對象實現一個 write 方法:
import sys class __redirection__: def __init__(self): self.buff='' self.__console__=sys.stdout def write(self, output_stream): self.buff+=output_stream def to_console(self): sys.stdout=self.__console__ print self.buff def to_file(self, file_path): f=open(file_path,'w') sys.stdout=f print self.buff f.close() def flush(self): self.buff='' def reset(self): sys.stdout=self.__console__ if __name__=="__main__": # redirection r_obj=__redirection__() sys.stdout=r_obj # get output stream print 'hello' print 'there' # redirect to console r_obj.to_console() # redirect to file r_obj.to_file('out.log') # flush buffer r_obj.flush() # reset r_obj.reset()
一樣的,sys.stderr, sys.stdin 也均可以被重定向到多個地址,觸類旁通的事情就本身動手實踐吧