成都创新互联网站制作重庆分公司

python将打印输出日志文件的方法

这篇文章主要介绍python将打印输出日志文件的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

成都创新互联是一家专业提供泰宁企业网站建设,专注与网站设计、做网站、H5技术、小程序制作等业务。10年已为泰宁众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

首先导入sys模块,然后利用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"

以上是python将打印输出日志文件的方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


文章标题:python将打印输出日志文件的方法
网址分享:http://cxhlcq.com/article/jdjied.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部