How to read text files using LINECACHE in Python

The linecache module in Python provides an efficient way to read specific lines from text files. It caches file contents in memory and allows random access to lines by their line number, making it ideal when you need to read multiple lines from the same file repeatedly.

Key Features of linecache

The linecache module offers several advantages ?

  • Memory caching: File contents are parsed and stored in memory
  • Line indexing: Access lines directly by line number (starting from 1)
  • Performance: Avoids repeatedly reading and parsing the same file

Creating Test Data

First, let's create a temporary file with sample text for demonstration ?

import os
import tempfile
import linecache

# Sample text for demonstration
sample_text = """Lorem ipsum dolor sit amet, causae apeirian ea his.
Id porro facete cum. No est veritus detraxit facilisis.
Te nam tempor posidonium scripserit, eam mundi reprimique.
Nec reque postea urbanitas ut, mea in nulla invidunt.
Atqui quaeque alienum te vim. Graeco aliquip liberavisse."""

def create_temp_file():
    """Create a temporary file with sample text"""
    fd, temp_file = tempfile.mkstemp(suffix='.txt')
    os.close(fd)
    
    with open(temp_file, 'w') as f:
        f.write(sample_text)
    
    return temp_file

def cleanup_file(temp_file):
    """Remove the temporary file"""
    os.unlink(temp_file)

# Create temporary file
filename = create_temp_file()
print(f"Created temporary file: {filename}")
Created temporary file: /tmp/tmpxyz123.txt

Reading Specific Lines

Use linecache.getline() to read a specific line by its number. Remember that line numbering starts from 1, not 0 ?

import linecache

# Read specific lines from the file
print("Line 1:", linecache.getline(filename, 1).strip())
print("Line 3:", linecache.getline(filename, 3).strip())
print("Line 5:", linecache.getline(filename, 5).strip())

# Reading a non-existent line returns empty string
print("Line 10:", repr(linecache.getline(filename, 10)))
Line 1: Lorem ipsum dolor sit amet, causae apeirian ea his.
Line 3: Te nam tempor posidonium scripserit, eam mundi reprimique.
Line 5: Atqui quaeque alienum te vim. Graeco aliquip liberavisse.
Line 10: ''

Handling Newlines and Empty Lines

The linecache module preserves newline characters at the end of each line ?

# Create file with empty lines
text_with_blanks = """First line
Second line

Fourth line (third was empty)
Fifth line"""

# Create another temp file
fd, temp_file2 = tempfile.mkstemp(suffix='.txt')
os.close(fd)

with open(temp_file2, 'w') as f:
    f.write(text_with_blanks)

# Read lines including empty ones
print("Line 1:", repr(linecache.getline(temp_file2, 1)))
print("Line 3 (empty):", repr(linecache.getline(temp_file2, 3)))
print("Line 4:", repr(linecache.getline(temp_file2, 4)))

# Cleanup
cleanup_file(temp_file2)
Line 1: 'First line\n'
Line 3 (empty): '\n'
Line 4: 'Fourth line (third was empty)\n'

Performance Comparison

Here's a comparison showing the efficiency of linecache for multiple reads ?

Method Memory Usage Best For
open().readlines()[n] Creates new list each time Single line reads
linecache.getline() Caches file in memory Multiple random access

Clearing the Cache

You can clear the cache manually when needed ?

# Clear cache for specific file
linecache.clearcache()

# Or clear cache for all files
linecache.checkcache()

# Cleanup our temporary file
cleanup_file(filename)
print("Temporary file cleaned up")
Temporary file cleaned up

Conclusion

The linecache module is perfect for applications requiring random access to file lines. It caches file contents for efficient repeated reads, but be mindful of memory usage with large files. Use it when you need to access multiple lines from the same file frequently.

Updated on: 2026-03-25T11:57:46+05:30

655 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements