python-3.xloopslogfiles

Print all print() in log file on exit


I have a script with a print in a while loop. This loop is running for like 10 hours and print something every minute or so. I would like to store all print() outputs in a single log file I don't know how to proceed I use windows and Python 3.6


Solution

  • print can send the output to a file. Just provide a file descriptor:

    for i in range(10): # or a `while` loop
        with open('mylog.log', 'a') as f:
            print("h0i", file=f)