- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
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.
If prettyprint of previous getline() function is no longer needed, you can clear the cache by this function.
This function checks if the cache is valid. This is useful if files in cache may have been changed on disk.
Seed the cache for filename with module_globals. The module loader will be asked for the source only when getlines is called, not immediately.
This function returns lines from file in the form of list object.
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.
- How to read text files using LINECACHE in Python
- Random Access Memory (RAM)
- Multi-access Channels and Random Access Channels
- Dynamic Random Access Memory (DRAM)
- How to write multiple lines in text file using Python?
- Java program to delete duplicate lines in text file
- How to sort lines of text files in Linux?
- How to display multiple lines of text in Tkinter Label?
- Counting number of lines in text file using java
- How to wrap long lines in Python?
- Random numbers in Python
- Lines and Indentation in Python
- What are different types of RAM (Random Access Memory) in computer architecture?
- How to count the number of lines in a text file using Java?