I am currently running a python script in a batch file. In the python, I have some print function to monitor the running code. The printed information then will be shown in the command window. In the meantime, I also want to save all these print-out text to a log-file, so I can track them in the long run.
Currently, to do this, I need to have both print function in the python and use the text.write function to write to a text file. This causes some troubles in maintenance because every time I change some printing text, I also need to change the text in the write function. Also I feel it is not the most efficient way to do that.
For example:
start_time = datetime.now()
print("This code is run at " + str(start_time) + "\n")
log_file.write("This code is run at " + str(start_time) + "\n")
I would like to use the print function in the python, so I can see that in the command window and then save all the print-out information to a log file at one time.