How to read a specific line from a text file in Python

To read a specific line from a text file in Python you can use readlines() or you can also import linecache. Here we gonna learn reading a specific line from a text file in Python for both large and small files.

We can achieve the line reading in Python with several methods but we will show you the easiest method first. Then we will see other methods.

In my previous Python tutorials, I have shown you several things you can do with a text file like

Read a specific line from a text file in Python

Code snippet:

file_variable = open('filename.txt')
all_lines_variable = file_variable.readlines()
print(all_lines_variable[specific_line_number - 1])

This is the easiest way to read a particular line from a text file in Python.

  • Where file_variable is the variable name.
  • all_lines_variable – This is the variable to hold the lines as an Object
  • all_lines[specific_line_number – 1] – If you want to read the line no 15 then put 14 in the square brackets.

A simple example of reading a particular line from a text file

Assume that we have a text file with the file name this_is_file.txt

Here is the content of the text file:

I am line no 1
I am line no 2
I am line no 3
I am line no 4
I am line no 5
I am line no 6
I am line no 7
I am line no 8
I am line no 9
I am line no 10

Now we have to read the texts from line no 5.

Then we will use the below code:

file = open('this_is_file.txt')
all_lines = file.readlines()


I am line no 5

Read a specific line from a text file in Python using linecache module

I love Python just because of its cool built-in modules.

Thankfully Python has linecache module which allows us to get any particular line from any file.

See an example,

Now, suppose you are told to read the line no 7 from the text file.

Here is the code:

import linecache


I am line no 7

To know more about the linecache module see the linecache documentation

6 responses to “How to read a specific line from a text file in Python”

  1. Nicolai Thomsen says:

    Thank you so much!

  2. Adrian says:

    Hello. Can you tell me how to read a text file starting from a specific line?

    Like, if it has line 1 to line 5, I want to start reading at line 2 onward till line 5 and ignore line 1.

  3. Mehregan Karbasi says:

    I learned sth but I’m sorry to say it does not help for large files like the fastText word embedding from facebook. The first method (read_lines) is a total deal breaker as it will need to hold all 2 million lines in memory and trust me when i say That no personal computer from 2011 (like my laptop) can handle.
    I tried using linecache and unfortunately it does not seem to work for .Vec files. I mean it did not print the line but still it took 79.41100716590881 seconds to process my python code. It should’ve done anything. my python code tried to print 250000th line.
    for reference, i did this with a for loop and it took 8 secs to process that same thing and it also was able to print the line.
    there’s more. it managed to print the 1900000th line in 79 secs. so we know that this method does not work for huge .vec files and if it does, it’s much slower than the simple for loop.
    Any ideas on that?

    • Saruque Ahamed Mollick says:

      I am not sure how to do that without storing the lines in memory. If you find a solution kindly put that here so that it helps other learners.

Leave a Reply

Your email address will not be published. Required fields are marked *