← All notes

linecache: read any line from a text file through a cache

Answer: The linecache module allows one to get any line from a Python source file and uses a cache. This is used by the traceback module to retrieve source lines. getline will never raise an exception.

Why the cache exists

The module gets any line from a Python source file and uses a cache for the common case where many lines are read from a single file.

The traceback module depends on this to retrieve source lines for a formatted traceback.

Files are opened with tokenize.open(), and tokenize.detect_encoding() picks the file encoding, which defaults to UTF-8 in the absence of an encoding token.

getline and the cache helpers

Call getline(filename, lineno, module_globals=None) to get line lineno from the named file.

This function will never raise an exception. On errors it returns '', and a successful line still includes its terminating newline.

clearcache() drops cached lines when you no longer need them. checkcache() revalidates entries if files may have changed on disk.

The same contract in the source docs

The library source documentation also says this is used by the traceback module to retrieve source lines.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, linecache — Random access to text lines; CPython, linecache module documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, linecache — Random access to text lines; CPython, linecache module documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.

Cite this

Permanent ID: iil:note:qv65vhjd

iinventedlight. 2026. "linecache: read any line from a text file through a cache." iinventedlight Library, iil:note:qv65vhjd, version of 2026-11-23. https://iinventedlight.com/library/notes/python-linecache/. License: CC-BY-4.0.

@misc{iil_note_qv65vhjd,
  author = {{iinventedlight}},
  title = {{linecache: read any line from a text file through a cache}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-23},
  url = {https://iinventedlight.com/library/notes/python-linecache/},
  note = {Permanent ID iil:note:qv65vhjd. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-linecache.md.