python把运行过程的print 输出到txt

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('log.txt')   #下面代码的print输出都会被输出到一个名称为log.txt的文本里面


print(path)
# print(os.path.dirname(__file__))
print('---------------------------------------------------------------------------------')
for i in range(5, 10):
    print("this is the %d times" % i)