linecache.getline() in Python

The linecache module in Python provides an efficient way to read specific lines from files. The linecache.getline() function retrieves a single line from any file while automatically handling caching for improved performance on subsequent reads.

Syntax

linecache.getline(filename, lineno, module_globals=None)

Parameters

  • filename ? Path to the file to read from
  • lineno ? Line number to retrieve (1-based indexing)
  • module_globals ? Optional global namespace for module files

Basic Example

Let's create a simple text file and retrieve a specific line ?

import linecache

# Create sample content (simulating test.txt)
content = """Line 1: This is the first line.
Line 2: This is the second line.
Line 3: This is the third line.
Line 4: This is the fourth line.
Line 5: This is the fifth line."""

# Write to file for demonstration
with open('test.txt', 'w') as f:
    f.write(content)

# Retrieve the third line
line = linecache.getline('test.txt', 3)
print(f"Retrieved line: {line.strip()}")
Retrieved line: Line 3: This is the third line.

Error Handling

When requesting a non-existent line, getline() returns an empty string instead of raising an error ?

import linecache

# Try to fetch a line that doesn't exist
result = linecache.getline('test.txt', 1000)
print(f"Non-existent line result: '{result}'")
print(f"Length of result: {len(result)}")

# Check if line exists
if result.strip():
    print("Line found")
else:
    print("Line not found")
Non-existent line result: ''
Length of result: 0
Line not found

Performance Benefits with Caching

The linecache module caches file contents in memory, making subsequent reads faster ?

import linecache
import time

# Create a larger file for demonstration
large_content = "\n".join([f"Line {i}: Sample content for line {i}" for i in range(1, 1001)])
with open('large_file.txt', 'w') as f:
    f.write(large_content)

# First read - file loaded into cache
start_time = time.time()
line1 = linecache.getline('large_file.txt', 500)
first_read_time = time.time() - start_time

# Second read - retrieved from cache
start_time = time.time()
line2 = linecache.getline('large_file.txt', 750)
second_read_time = time.time() - start_time

print(f"First read (line 500): {line1.strip()}")
print(f"Second read (line 750): {line2.strip()}")
print(f"First read time: {first_read_time:.6f} seconds")
print(f"Second read time: {second_read_time:.6f} seconds")
First read (line 500): Line 500: Sample content for line 500
Second read (line 750): Line 750: Sample content for line 750
First read time: 0.001234 seconds
Second read time: 0.000098 seconds

Reading from Multiple Files

You can efficiently read lines from different files using the same function ?

import linecache

# Create multiple sample files
files_data = {
    'file1.txt': 'First file, line 1\nFirst file, line 2',
    'file2.txt': 'Second file, line 1\nSecond file, line 2',
    'file3.txt': 'Third file, line 1\nThird file, line 2'
}

# Write sample files
for filename, content in files_data.items():
    with open(filename, 'w') as f:
        f.write(content)

# Read first line from each file
filenames = ['file1.txt', 'file2.txt', 'file3.txt']

for filename in filenames:
    first_line = linecache.getline(filename, 1)
    print(f'First line in {filename}: {first_line.strip()}')
First line in file1.txt: First file, line 1
First line in file2.txt: Second file, line 1
First line in file3.txt: Third file, line 1

Key Points

  • Uses 1-based indexing (first line is line 1, not 0)
  • Returns empty string for non-existent lines or files
  • Automatically caches file contents for performance
  • Preserves newline characters in returned strings
  • Works with any text file, including Python source files

Conclusion

The linecache.getline() function provides an efficient way to read specific lines from files with built-in caching. It's particularly useful for applications that need to access random lines from large files repeatedly without loading the entire file into memory manually.

Updated on: 2026-03-27T08:20:47+05:30

781 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements