Python中将打印输出日志文件


一. 利用sys.stdout将print行导向到你定义的日志文件中,例如:

import sys # make a copy of original stdout route stdout_backup = sys.stdout
# define the log file that receives your log info log_file = open("message.log", "w")
# redirect print output to log file sys.stdout = log_file print "Now all print info will be written to message.log" # any command line that you will execute ... log_file.close() # restore the output to initial pattern sys.stdout = stdout_backup print "Now this will be presented on screen"


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM