Print all print() in log file on exit

99 Views Asked by At

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

1

There are 1 best solutions below

0
Highstaker On BEST ANSWER

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)