在Python中如何重定向標准輸出stdout到文件代碼示例:
- import sys
-
- oldStdout = None
- logfile = None
- try:
- logfile = open('d:/1.log','w+')
- oldStdout = sys.stdout
- sys.stdout = logfile
- print 'Hello World in File Log!'
- finally:
- if logfile:
- logfile.close()
- if oldStdout:
- sys.stdout = oldStdout
-
- print 'Hello World in Screen!'