• 技术文章 >常见问题 >Python常见问题

    python怎么将打印输出日志文件

    爱喝马黛茶的安东尼爱喝马黛茶的安东尼2019-11-04 09:26:51原创2734

    首先导入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平台,欢迎关注!

    专题推荐:python 打印 日志文件
    品易云
    上一篇:Python的pip无法升级怎么办 下一篇:win10怎么添加python环境变量

    相关文章推荐

    • python如何打印日志• python怎么打印日志• python怎么记录日志

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网