Python - Traceback module

Posted on Mon 16 January 2017 in Python, traceback

How to print current call stack for debugging purpose

import traceback
def f():
    g()
def g():
    for line in traceback.format_stack():
        print(line.strip())
f()

It gives output as follows :

# Prints:
# File "example_tracestack.py", line 10, in 
#     f()
# File "example_tracestack.py", line 4, in f
#     g()
# File "exampel_tracestack.py", line 7, in g
#     for line in traceback.format_stack():