Random access to text lines in Python (linecache)

The linecache module in Python's standard library provides random access to any text file by line number. This module is extensively used by Python's traceback system to generate error traces and caches previously read lines to improve performance when reading lines repeatedly.

Key Functions

getline(filename, lineno)

Returns the specified line number from a file. If the line doesn't exist, it returns an empty string. If the file isn't in the current directory, it searches in sys.path.

getlines(filename)

Returns all lines from a file as a list object.

clearcache()

Clears the internal cache when previous cached data is no longer needed.

checkcache(filename=None)

Checks if cached data is still valid, useful when files may have been modified on disk.

Creating a Sample File

First, let's create a sample text file containing the Zen of Python ?

import sys
import io
import linecache

# Capture the Zen of Python
zen = io.StringIO()
old_stdout = sys.stdout
sys.stdout = zen
import this
sys.stdout = old_stdout

# Write to file
with open('zen.txt', 'w') as f:
    f.write(zen.getvalue())

print("zen.txt file created successfully!")
zen.txt file created successfully!

Reading Specific Lines

Now we can use getline() to read any line by its number ?

import linecache

# Read the 4th line
line4 = linecache.getline('zen.txt', 4)
print(f"Line 4: {repr(line4)}")

# Read the 7th line
line7 = linecache.getline('zen.txt', 7)
print(f"Line 7: {repr(line7)}")
Line 4: 'Explicit is better than implicit.\n'
Line 7: 'Flat is better than nested.\n'

Reading Multiple Lines

Use getlines() with slicing to read a range of lines ?

import linecache

# Get lines 4 to 8
lines = linecache.getlines('zen.txt')[4:9]
for i, line in enumerate(lines, 5):
    print(f"Line {i}: {line.strip()}")
Line 5: Simple is better than complex.
Line 6: Complex is better than complicated.
Line 7: Flat is better than nested.
Line 8: Sparse is better than dense.
Line 9: Readability counts.

Cache Management

The linecache module automatically caches file contents for better performance ?

import linecache

# Read a line (this caches the file)
line = linecache.getline('zen.txt', 1)
print(f"First line: {line.strip()}")

# Check if cache is valid
linecache.checkcache()
print("Cache checked and validated")

# Clear the cache when done
linecache.clearcache()
print("Cache cleared")
First line: The Zen of Python, by Tim Peters
Cache checked and validated
Cache cleared

Conclusion

The linecache module provides efficient random access to text files by line number with automatic caching. It's particularly useful for debugging tools and applications that need to repeatedly access specific lines from large files without loading the entire file into memory.

---
Updated on: 2026-03-25T05:08:03+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements