How to read specific lines from a file (by line number)?
To read lines 2 and 4 from my_file.txt
, use a list to specify line numbers and enumerate
within a loop to filter them:
Here, enumerate
keeps track of the line numbers, the condition if i in lines_to_read
ensures we read only the required lines, and line.strip()
removes all that undesired leading whitespace.
Reading lines smarter, not harder: optimum approach for large files
For larger files, we want to minimize both memory usage and run-time. A for loop along with enumerate
plays this out by stopping the loop as soon as we read all targeted lines:
The if-elif
logic ensures that unnecessary lines are skipped over, making the reading quicker and more memory-efficient.
Taking control: reading and processing specific lines dynamically
Need to process lines as they come or wish to select lines based on complex criteria? Generator expressions offer a compact and memory-efficient solution:
This code yields one line at a time, perfect for processing lines on-the-fly rather than collecting all at once.
When you need a hand: using external libraries for efficiency
Handling multiple files or frequently accessing different lines? linecache
can be your best friend, fetching any line from any file with minimal code:
With linecache
, remember it counts lines starting from 1, just like us humans.
Range reading: when you need more than one
Need to read a range of lines? Use islice
from itertools. It's like slicing a pizza, but with files:
Using islice
, you can extract lines without reading the entire file into memory — an efficient 'read-once' approach.
Memory or speed: knowing when to opt for what
Deciding between simplicity and efficiency can often depend on the size of your file and nature of your task.
For humongous files, this can be memory exhausting, choose your method while considering your resources closely!
Picking the right tool for the task
A quick guide on choosing the right method:
- Tiny files: Direct indexing after
file.readlines()
. - Gigantic files, few lines:
enumerate
with afor loop
. - Multiple files or recurring access:
linecache
. - Complex line selection: Generators or
islice
. - Range of lines:
islice
.
Don't forget to close your files or use the with
keyword to let Python do the needful!
Was this article helpful?