---
source: https://iinventedlight.com/library/notes/python-linecache.md
title: "linecache: read any line from a text file through a cache"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF License Version 2 (Python Software Foundation); PSF License Version 2 (CPython); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-23
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-23
as_of: 2026-11-23
confidence: medium
untested: "Not reproduced (2026-11-23). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: python-linecache"
tags: [python, linecache, traceback, files]
client: Librarian
sources:
  - id: S1
    title: "linecache — Random access to text lines"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/linecache.html
    accessed: 2026-11-23
    license: "PSF License Version 2"
  - id: S2
    title: "linecache module documentation (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/linecache.rst
    accessed: 2026-11-23
    license: "PSF License Version 2"
---
# 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

- [python-traceback](https://iinventedlight.com/library/notes/python-traceback/)
- [python-inspect](https://iinventedlight.com/library/notes/python-inspect/)

## Claims

- id: C1
  claim: linecache gets any line from a Python source file and caches repeated reads.
  source: S1
  evidence: "get any line from a Python source file"

- id: C2
  claim: traceback uses linecache to retrieve source lines.
  source: S1
  evidence: "used by the traceback module to retrieve source lines"

- id: C3
  claim: getline never raises; it returns an empty string on errors.
  source: S1
  evidence: "This function will never raise an exception"

- id: C4
  claim: The source docs also describe the cache for many lines from one file.
  source: S2
  evidence: "many lines are read from a single file"

- id: C5
  claim: The source docs also say traceback retrieves source lines through this module.
  source: S2
  evidence: "retrieve source lines for inclusion in the formatted traceback"

## Sources

- [S1] linecache — Random access to text lines. Python Software Foundation. https://docs.python.org/3/library/linecache.html . License: PSF License Version 2. Accessed 2026-11-23.

- [S2] linecache module documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/linecache.rst . License: PSF License Version 2. Accessed 2026-11-23.

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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
