How to log every error in file you get in cmd

Question:

I tried try catch block in my python script but my team lead wants to log every error which can occure i cannot write try catch every line do anyone know better way to do this? currently im using try catch and logger module for logging certain comparisions she says she wants to log everytime error happens

only getting log of try catch block

Asked By: Manav Suhange

||

Answers:

If all you need to do is log when an error happens, then I’d just wrap the entire script (or at least the part that is going to produce the error) in a single try-catch block that simply writes to a file whenever an error occurs.

try:
    # all your code here
except:
    with open("log.txt", "a") as fp:
        fp.write("error")
        fp.write(data)
Answered By: user911346