lang/python/ TracebackModule


See docs here. Note that sys.exception(), within an except, returns the current exception.

import traceback
from datetime import datetime
try:
  open("does_not_exist")
except Exception as e:
  traceback.print_exc()
  with open("exc.txt","at") as f:
    print(f"==[ {datetime.now().strftime('%c')} ]==",file=f)
    traceback.print_exc(file=f)