- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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.
- Related Articles
- How to read text files using LINECACHE in Python
- How to write multiple lines in text file using Python?
- Multi-access Channels and Random Access Channels
- Random Access Memory (RAM)
- Dynamic Random Access Memory (DRAM)
- How to Copy Odd Lines of Text File to Another File using Python
- Java program to delete duplicate lines in text file
- How to display multiple lines of text in Tkinter Label?
- How to sort lines of text files in Linux?\n
- How to access Python objects within objects in Python?
- How to set the background colour of text lines with Text using FabricJS?
- Access to the Password Database in Python
- Access to the Group Database in Python
- Random numbers in Python
- What are different types of RAM (Random Access Memory) in computer architecture?