Explain Codes LogoExplain Codes Logo

How to read specific lines from a file (by line number)?

python
prompt-engineering
best-practices
memory-optimization
Alex KataevbyAlex Kataev·Feb 20, 2025
TLDR

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:

lines_to_read = [2, 4] # only these lines please! result = [line.strip() for i, line in enumerate(open('my_file.txt'), 1) if i in lines_to_read] print(result) # voila! we have got the lines!

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:

lines_to_read = set([2, 4]) # only these and nothing more! with open('large_file.txt') as file: # let's dive into the ocean! result = [] # an empty container for our precious lines for i, line in enumerate(file, 1): # here we go, counting lines... if i in lines_to_read: # bingo! found one of the lines! result.append(line.strip()) # "bag 'em and tag 'em," as gamers would say elif i > max(lines_to_read): # nothing more we need, time to break the loop break print(result) # tasty lines served!

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:

lines_to_read = [2, 4] with open('file.txt') as file: lines = (line.strip() for i, line in enumerate(file, 1) if i in lines_to_read) # generator, please stand by for line in lines: # iterate through the generator print(line) # make it speak!

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:

import linecache line_number = 3 desired_line = linecache.getline('file.txt', line_number).strip() print(desired_line) # jackpot! here's your line

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:

from itertools import islice start_line = 2 end_line = 4 with open('file.txt') as file: selected_lines = islice(file, start_line - 1, end_line) # and sliceeeee... for line in selected_lines: print(line.strip()) # sweet lines, hot out of the oven!

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.

with open('small_file.txt') as file: all_lines = file.readlines() # read it all in one go. print(all_lines[1], all_lines[3]) # print exactly what we need.

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 a for 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!