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

13 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?

  4. Nintendo says:

    I want to search a specific line with the string name and change can i do that?..
    Like in a text file if there is a line Apple=Fruit…i want to search this line whereever it is in the file and change it to Apple = Tasty can i do that??
    Please help me

  5. Robert says:

    Am I able to set a variable as a specific line in another file? For example, if line 7 in a file said “hello” could I set a variable to what line 7 says so that if I were to for example print that variable, it would also say “hello”?

  6. Naman Singh Shekhawat says:

    I want to know how can I find the line no. from a particular file. Like if my file as a list of names, I would like to know which specific line does a name appear.(P.S: This code was also really helpful!)

  7. C,Sudhakar says:

    This is very useful for a beginner like me. Thank you so much,Keep writing more to help people like me.

  8. Maricco says:

    Hi – how to count a given two or three values all found in the same line as one line. Example, five single sporty ladies. Values are five, single, ladies but will be counted just one 1 line. Thanks in advance.

Leave a Reply