traceback.format_exc()
can get it with raising an exception.
traceback.print_stack()
prints the stack without an exception needed, but it does not return a string.
There doesn't seem to be a way to get the stack trace string without raising an exception in python?
Method 1: By using print_exc() method. This method prints exception information and stack trace entries from traceback object tb to file.
Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.
It's traceback.extract_stack()
if you want convenient access to module and function names and line numbers, or ''.join(traceback.format_stack())
if you just want a string that looks like the traceback.print_stack()
output.
How about traceback.format_stack?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With