Random access to text lines in Python (linecache)


Purpose of linecache module in Python’s standard library is to facilitate random access to any text file, although this module is extensively used by Python’s traceback module to generate error trace stack. Further prettyprints of reading are held in a cache so that it saves time while reading lines repeatedly.

The most important function in this module is getline() which reads a specified line number from given file. Following is the list of functions −

getline(file, x)

This function returns xth line from file. If not present it will return empty string. If the file is not present in current path, function ties to locate it in directories in sys.path – the module search path.

clearcache()

If prettyprint of previous getline() function is no longer needed, you can clear the cache by this function.

checkcache()

This function checks if the cache is valid. This is useful if files in cache may have been changed on disk.

lazycache()

Seed the cache for filename with module_globals. The module loader will be asked for the source only when getlines is called, not immediately.

getlines()

This function returns lines from file in the form of list object.

updatecache()

This function updates cache entries and returns a list of lines.

To demonstrate use of linecache functionality, first we build a text file to store the famous Zen of Python (list of software principles that influence design philosophy of Python). Output of ‘import this’ is redirected to zen.txt by following code −

import sys, io
zen = io.StringIO()
old_stdout = sys.stdout
sys.stdout = zen
import this
sys.stdout = old_stdout
f=open('zen.txt','w')
f.write(zen.getvalue())
f.close()

When above code is executed, zen.txt will be created in current directory. We shall use this text file to read lines from it with getline() function.

To read 4th line from file

>>> linecache.getline('zen.txt',4)
'Explicit is better than implicit.\n'

Note that the return string ends with newline character.

To display line numbers from 4 to 10, use slice operator on list returned by getlines() function

>>> linecache.getlines('zen.txt')[4:10]
['Simple is better than complex.\n', 'Complex is better than complicated.\n', 'Flat is better than nested.\n', 'Sparse is better than dense.\n', 'Readability counts.\n', "Special cases aren't special enough to break the rules.\n"]

In this article we learned about linecache module in Python standard library.

Updated on: 25-Jun-2020

670 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements