try:
    f = open('nonExistant file', 'r')
except:
    exc_type, exc_value, exc_traceback = sys.exc_info()
print(exc_type)
tb_list = traceback.extract_tb(sys.exc_info()[2])
tb_list = traceback.format_list(tb_list)
for elt in tb_list:
    pass
How can you programmatically inspect the stack trace of an exception in Python?