Python Glossary Русский

all Python 3tracebackfunction

traceback.print_tb()

printing tracebacks

Prints only the frames of a traceback — no header and no exception type line — from an e.__traceback__ object.

Syntax

traceback.print_tb(tb, limit=None, file=None)

It takes a traceback, not an exception. Unlike print_exception there is neither "Traceback (most recent call last):" nor "ValueError: …" — only File/line entries. A positive limit keeps the first N frames from where it was caught, a negative one (since 3.5) the last N near the error, usually the interesting ones.

Examples

import io, traceback
def a():
    b()
def b():
    raise KeyError('id')
try:
    a()
except KeyError as e:
    buf = io.StringIO()
    traceback.print_tb(e.__traceback__, limit=-1, file=buf)
print(buf.getvalue().splitlines()[1].strip())  # → raise KeyError('id')

See also

Open in the full glossarydocs.python.orgReport an inaccuracy