Python Glossary Русский

all Python 3tracebackfunction

traceback.format_tb()

formatting tracebacks

Only the traceback frames as strings — without the "Traceback (most recent call last):" header and without the type-and-message line.

Syntax

lines = traceback.format_tb(tb, limit=None)

It takes the traceback object e.__traceback__, not the exception. It is essentially format_list(extract_tb(tb)). Use it when the type and message are printed separately in your own report format; format_exception() gives the full picture in one call.

Examples

import traceback
try:
    [][0]
except IndexError as e:
    parts = traceback.format_tb(e.__traceback__)
print(len(parts), parts[0].startswith('  File'))  # → 1 True
print('Traceback' in ''.join(parts))  # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy