How to log error to file, and not fail on exception

后端 未结 4 2002
忘了有多久
忘了有多久 2021-01-31 16:55

I am downloading a file from the net, and it fails even though I am doing:

for p in query:

try:

except IOError as e:
   print e;

If th

4条回答
  •  萌比男神i
    2021-01-31 17:34

    This will write your error to a log file and continue to run the code.

    import traceback
    
    #This line opens a log file
    with open("log.txt", "w") as log:
    
        try:
            # some code
            # Below line will print any print to log file as well.
            print("Creating DB Connection", file = log)
        except Exception:
            traceback.print_exc(file=log)
            continue
    

提交回复
热议问题