The program needs to run for a long time in the project, and then troubleshoot errors according to the output information. Because the IDE console and CMD window can only save a small part of the current output information, it is not conducive to debug. Therefore, it is important to store all the output locally so that errors can be traced back. Document the implementation method here.

Redirect the standard output stream (print)

F = open('a. TXT ', 'w') print('python is good', file=f, flush=True

Redirect the standard output stream (suitable for saving all print)

Import sys f = open('a.log', 'a') # a.log or a.txt import sys. Stdout = f sys. Stderr = f... F.flush ()#, as above, ensure that you save the previous statements to a file when you run here, otherwise you will only start writing file actions when the program ends or f.lose

Create a custom Logger

class Logger(object): def __init__(self,fileN ="Default.log"): self.terminal = sys.stdout self.log = open(fileN,"a") def write(self,message): self.terminal.write(message) self.log.write(message) def flush(self): Pass import sys sys.stdout = Logger("./1.txt") pass import sys sys.stdout = Logger("./1.txt") Print () 'to the'

https://www.jb51.net/article/…