This article lets you know how to read a specific lines from a file by line number in Python.
Table of contents
- Steps To Read Specific Lines From A File
- linecache Module Read line from a file by line number
- Use readlines() to Read the range of line from the File
- Generator to Read Lines from a file by line number
- for Loop in fileobject to Read Specific Lines in Python
Steps To Read Specific Lines From A File
Let’s assume the file to read is significantly large (in GB), and you don’t want to read the whole file in memory at once but only want to jump and read lines #5 and #120. To read specific lines from a text file, Please follow these steps:
- Open file in Read Mode
To open a file pass file path and access mode
open()function. The access mode specifies the operation you wanted to perform on the file, such as reading or writing.
fp= open(r'File_Path', 'r')to read a file.
- Create a list to store line numbers
Create a list with the number of each line in a text file to read.
line_numbers = [4, 7]. Here we are reading lines 4 and 7.
- Create a list to store lines
After reading line 4 and 7 we will store result it in a list variable.
- Use for loop with enumerate() function to get a line and its number.
The enumerate() function adds a counter to an iterable and returns it in enumerate object. Pass the file pointer returned by the
open()function to the
We can use this enumerate object with a for loop to access the line number.
enumerate(file_pointer)doesn’t load the entire file in memory, so this is an efficient solution.
- Read file by line number
Use the if condition in each iteration of a loop to check the line number. If it matches, then save that line into a list.
Example: Read specific lines from file by line number
The following code shows how to read a text file by line number in Python. See the attached file used in the example and an image to show the file’s content for reference.
In this example, we are reading line number 4 and 7 and storing it in a list variable.
with open(r"E:\demos\files\read_demo.txt", 'r') as fp: # lines to read line_numbers = [4, 7] # To store lines lines =  for i, line in enumerate(fp): # read line 4 and 7 if i in line_numbers: lines.append(line.strip()) elif i > 7: # don't read after line 7 to save time break print(lines)
['Fifth line', 'Eighth Line']
linecache Module Read line from a file by line number
In this section, we’ll see how to read file by line number using a linecache module.
Python’s linecache is another performance-optimized way to jump to a particular line in a text file. Assume if you have a large text file, then linecache is the correct choice.
The linecache module allows one to get any line from a Python source file, while attempting to optimize internally, using a cache, the common case where many lines are read from a single file. This is used by the traceback module to retrieve source lines for inclusion in the formatted traceback.Python Documentation
linecache.getline() method to read specific line from a file.
linecache.getline(filename, lineno, module_globals=None)
- Get line
linenofrom a file named
filename. This function will not return any error if the line is not present in a file instead, it will return an empty string.
- Change the
linenoto your desired line number, and you’re ready to go.
import linecache # read fifth line line = linecache.getline(r"E:\demos\files\read_demo.txt", 5) print(line)
Note: The linache reads the whole file in memory. So, if random access to line number is more important than performance, then use linache.
If you want to read more than one line number from a file using linecache use the below example.
import linecache line_numbers = [2, 4] lines =  for i in line_numbers: x = linecache.getline(r"E:\demos\files\read_demo.txt", i).strip() lines.append(x) print(lines)
readlines() to Read the range of line from the File
If your file size is small and you are not concerned with performance, then the readlines() method is best suited.
Reading a file in Python is fast if the file size is in a few MB.
readlines() method reads all lines from a file and stores it in a list. You can use an index number as a line number to extract a set of lines from it.
This is the most straightforward way to read a specific line from a file in Python. We read the entire file using this way and then pick specific lines from it as per our requirement.
readlines()[start:end] to read range of lines.
- the start is the starting line number
- the end is the last line number
- To read from line number 3 to 5 use
- To read a single line use
fp.readlines(). this will read the third line.
Example: Read line from 3 to 5
with open(r"E:\demos\files\read_demo.txt", 'r') as fp: # read line number 3 to 5 # index starts from 0 x = fp.readlines()[2:5] print(x)
Example: Read line 8
with open(r"E:\demos\files\read_demo.txt", 'r') as fp: # read line 8 x = fp.readlines() print(x)
You can also use the
readline() method to read a file line by line, stop when you’ve gotten to the lines you want. Using this technique, we don’t need to read the entire file.
lines = [2, 5] result =  i = 0 with open("read_demo.txt", "r+") as fp: # access each line while True: line = fp.readline() # check line number if i in lines: result.append(line.strip()) # line number greater than needed exit the loop # lines[-1] give last item from list if i > lines[-1]: break; i = i + 1 print(result)
Generator to Read Lines from a file by line number
A fast and compact solution could be a generator expression to read a file using the line numbers.
If the number of lines to be returned from a file vast, you should use the generator.
This solution accepts file pointer and line numbers to be read returns a generator object to iterate using a loop to get each line.
# read by line numbers def get_lines(fp, line_numbers): return (x for i, x in enumerate(fp) if i in line_numbers) with open(r"E:\demos\files\read_demo.txt", 'r') as fp: # read line 4 and 7 lines = get_lines(fp, [3, 6]) # print each line for line in lines: print(line.strip())
Fourth line Seventh Line
for Loop in
fileobject to Read Specific Lines in Python
If your file size is small and you are not concerned with performance, then use looping technique.
- Use for loop to iterate each line from a file object
- Use the if condition in each iteration of a loop to check the line number. If it matches, then save that line into a list.
Note: This will load entire file in memory.
lines = [2, 5] result =  i = 0 with open("read_demo.txt", "r+") as fp: # access each line for line in fp: # check line number if i in lines: result.append(line.strip()) i = i + 1 print(result)
['Third line', 'Sixth Line']
readline()and loop to iterate lines from a file object if the file size is small.
- Use linecache for a more clean solution. It is fast if you are reading repeatedly or reading different lines from multiple files.
- Use a loop and
enumerate()for large files because we don’t need to load the entire file in memory.