About usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

Read a File Line by Line in Python

When reading a text file, sometimes we need to take the text one line at a time. This article will discuss some tools Python offers to help facilitate the reading of a file, line by line. In this article, we’ll cover:

  • How to Read a File Line by Line in Python?
  • Using with open() Statement 
  • Using the readlines() Method
  • Using the fileinput Module for Large Files
  • Using for Loop
  • Using while Loop
  • FAQs 

How to Read a File Line by Line in Python?

To read a file line by line in Python, we can use several methods, but the first step in all the methods is to open the file using the open() function. Let’s briefly look at how this open function works:

Open Function

  • We can simply open a file in any particular mode using the following syntax:

open("filePath", "mode")

  • Example: Opening a text file in read mode.

open("Documents\\test.txt", "r")

  • The open function opens the desired file and returns it as a file object. It can open the file in various modes, which we can optionally specify using the following characters: 

t

Default, text mode

r

Read mode

w

Write mode

+

Update mode: read+write mode

x

Creates file, returns an error if file exists

a

Write mode, the content will be appended at the end, creates the file if it doesn’t exist.

b

Binary Mode

rb

Read Binary Mode

wb

Write Binary Mode

  • The open function takes the text file’s system path as appropriate within the directory we’re running the code from, and if we want to open the file specifically in read mode, we add "r" to specify that.
  • For example, if you’re inside C:\Users\91675 when we’re running this python code when our text file is present at C:\Users\91675\Documents\test.txt, the path shown in the example above would suffice. It’d help to give the full file path if you aren’t sure about the current directories and paths. As you can see, the double backslash is required in such cases since \t would otherwise act as an escape character (tab). A forward slash instead of a double slash will work too.

Handling Exception

If the file is not found in the system path mentioned, we’ll get a FileNotFoundError exception. We need to handle this exception, which we can do in the following way:

try:

   open("Documents\\test2.txt", "r")

except FileNotFoundError as not_found:

   print("Incorrect file name or path given")

Now that we’ve learned how to open our file for reading, let us look at some ways to read it line by line.

Using “with open()” Statement

One way to read a file line by line in Python is by using with open() statement. The keyword “with” automatically closes the file once the execution is successful. We already know about the open function. Once we open the file, we can use a for loop to iterate through the file line by line and print each line as shown:

with open("Documents\\test.txt", "r") as file:

  for line in file:

    print(line)

Using the readlines() Method

Another way to read a file line by line in Python is by using the readlines() function, which takes a text file as input and stores each individual line as an element in a list. We can read a file into a list or an array using the readlines() method as shown: 

#Into a list

openFile = open("Documents\\test.txt","r")

asList = openFile.readlines()

print(asList)

openFile.close()


#Into an array

asArray = []

with open("Documents\\test.txt", "r") as file:

    asArray = file.readlines()

print(asArray)

We can also mention the size of line we want to read inside readlines as readlines(length) and it will only read the line till the desired length:

#Mentioning size

openFile = open("Documents\\test.txt","r")

asList = openFile.readlines(15)

print(asList)

openFile.close()

So if the file openFile contains an address in a few lines, this will only return 0 to 15 characters of the first line of the address. If the first line has more characters than the limit dictates, it will not return address line 2. That is because we’ve limited the length until which the readlines() method needs to read. 

If the first line contains fewer characters than the number we have specified, this function keeps on reading lines until the total size of the lines read so far doesn’t exceed the limit we have set. For example, if we have three lines containing four characters each and we specify the limit as 6, then the first two lines will be read.

Using “for” Loop

As we saw in the with open() statement example, since the open function returns an iterable, we can iterate through the file line by line. In this case, we will just use the for loop and not use with open() statement. We will open the file, iterate through it, and separately close the file as shown: 

openFile = open("Documents\\test.txt","r")

for line in openFile:

    print(line)

openFile.close()

Using “while” Loop

We may also use a while loop to read a file line by line by using the readline() function within it, which reads a text file line by line and returns all lines as strings: 


openFile = open("Documents\\test.txt","r")

while openFile:

    line  = openFile.readline()

    print(line)

    if line == "":

        break

openFile.close()


When the reading of all the lines is completed, readLine returns an empty string. That is why here we break the while loop when we read an empty string.

Using fileinput Module for Large Files

Since the readlines() method works by loading the whole file into memory and then iterating over it, if a file is large enough, using the readlines() method can lead to MemoryError. For such cases, we can use the input() method in the fileinput module, which reads line by line but doesn’t store the lines in memory after their reading is done. We can also use the for and while loop method over here since we’re reading one line at a time and not storing all the data.

import fileinput

for line in fileinput.input(["Documents\\test.txt"]):

    print(line)

FAQs

Question 1: Do we have to add a comma to indicate the open function takes two parameters if we’re not mentioning the mode within it?

No, it is neither essential to mention the mode nor is it essential to add a comma to indicate it takes two parameters. Although we can add some or all of these, and it will work just as well. All these three versions will run just fine:

openFile = open("Documents\\test.txt","r")

openFile = open("Documents\\test.txt",)

openFile = open("Documents\\test.txt")

Question 2: What is the difference between the read(), readline() and readlines() methods?

While we use all three to read text files, there’s a difference in how they work to achieve that goal:

The read() method

The readlines() method

The readline() method

Reads the whole file’s text into a string

Reads all the lines in a file and returns a list of strings containing the read lines as the elements

Reads the file line by line and returns all the lines as strings

Recommended Reading

Are you a software engineer looking for coding interview questions to aid your interview prep? Check out these useful pages for software developers:

Are You Ready to Nail Your Next Coding Interview?

Whether you’re a Coding Engineer gunning for Software Developer or Software Engineer roles, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!

If you’re looking for guidance and help with getting started, sign up for our free webinar. As pioneers in the field of technical interview prep, we have trained thousands of Software Engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!

Sign up now!

————

Article contributed by Tanya Shrivastava

Attend our Free Webinar on How to Nail Your Next Technical Interview

Recommended Posts

All Posts