打赏

星辰大海ゞ

That which does not kill us makes us stronger!

导航

Python将print输出内容保存到指定文件中

 

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import os

class Logger(object):
    def __init__(self, filename="Default.log"):
        self.terminal = sys.stdout
        self.log = open(filename, "a")

    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)

    def flush(self):
        pass
path = os.path.abspath(os.path.dirname(__file__))
type = sys.getfilesystemencoding()
sys.stdout = Logger('/usr/lib/zabbix/alertscripts/log')

print(path)
print(os.path.dirname(__file__))

 

posted on 2019-06-03 13:20  星辰大海ゞ  阅读(17727)  评论(1编辑  收藏  举报