Every time it is input to a txt file, there is only the last piece of data or the piece of data just started, not all the data, so it is very annoying
I found a good code, let me say it first: I copied and pasted it directly
import sys, os, time
# log recorder
class Logger(object):
def __init__(self, stream=sys.stdout):
output_dir = "./75_logpool/" # folder
if not os.path.exists(output_dir):
os.makedirs(output_dir)
#log_name = '{}.txt'.format(time.strftime('%Y-%m-%d-%H-%M',time.localtime(time.time())))
log_name_time = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
log_name = log_name_time + ".txt"
filename = os.path.join(output_dir, log_name)
self.terminal = stream
self.log = open(filename, 'a+')
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
pass
The following is the main function code called:
sys.stdout = Logger(sys.stdout) # record log
sys.stderr = Logger(sys.stderr) # record error
The following is someone else's content, I directly took a screenshot
because the content of this blog was written by me, but part of it was borrowed from other people's stuff. Therefore, I still say that the blog I wrote is original, thank you!
The blog address I refer to is as follows:
https://zhuanlan.zhihu.com/p/561010832