In this article we will discuss different ways to read a file line by line in Python.

Suppose we have a file data.txt in same directory as our python script. Let’s see how to read it’s contents line by line.

Solution for Small Files : Get list of all lines in file using readlines()

First basic and inefficient solution is using function readlines().

If we have a small file, then we can call readlines() on the file handler, it reads the whole file content to memory, then splits it into seperate lines and returns a list of all lines in the file. All the lines in list except the last one, will contain new line character in end.
For example,

It will return a list of lines in file. We can iterate over that list and strip() the new line character then print the line i.e.

Output:

But if file size is large then it will consume a lot of memory, so better avoid this solution in case of large files.

Let’s look at some efficient solutions,

Read a file line by line using readline()

While Reading a large file, efficient way is to read file line by line instead of fetching all data in one go.
Let’s use readline() function with file handler i.e.

readline() returns the next line in file which will contain the newline character in end. Also, if end of file is reached then it will return an empty string.
Now let’s see how to read contents of a file line by line using readline() i.e.

Output:

Read file line by line with context manager (with block)

When we open the file then we need to close that too. If we forget to close then it will be closed automatically when last reference to file handler is destroyed for example at the end of function. But what if we have a large function that is not going to end soon, even if file related work is complete. In that case we can use context manager to automatically cleanup the things like file closure etc.
For example,

Output:

In this case when control comes out of with block then file will be automatically closed. Even if it came out of block due to some exception.

Get List of lines in file with context manager (with block)

Let’s Iterate over all the lines in file and create a list of lines i.e.

Contents of the list listOfLines will be,

Read contents of file line by line using with context manager and while loop

Let’s iterate over the lines in file with context manager and while loop i.e.

Contents of the list will be,

Complete example is as follows,

Output:

 

Click Here to Subscribe for more Articles / Tutorials like this.