Python - Read Text File Line by Line


Python - Read File Line by Line

There are many ways to read a text file line by line in Python. You can read the lines to a list, or just access them one by one in a loop by iterating over the lines provided by some kind of iterator or calling a function on the file object.

In this tutorial, we will learn how to read a file line by line using readline() function, readlines() function, or file object, with the help of example programs.

Example 1: Read Text File Line by Line - readline()

In this example, we will use readline() function on the file stream to get next line in a loop.

Steps to use file.readline() function

Following are the steps to read file line by line using readline() function.

  1. Read file in text mode. It returns a stream to the file.
  2. Create an Infinite While Loop.
    1. During each iteration of the loop, read the next line from the file using readline().
    2. If the line is not empty, you have the next line. You can check this using if-not. Else, there are no more lines in the file, and we break the loop.
  3. By the time we break out of the loop, we have read all the lines of file one by one during the iterations.
  4. Since we are done with the file, we will close it.

Python Program

#get file object
file1 = open("sample.txt", "r")

while(True):
	#read next line
	line = file1.readline()
	#check if line is not null
	if not line:
		break
	#you can access the line
	print(line.strip())

#close file
file1.close

Output

Hi User!
Welcome to Python Examples.
Continue Exploring.

Example 2: Read Lines as List - readlines()

readlines() function returns all the lines in the file as a list of strings. We can traverse through the list, and access each line of the file.

In the following program, we shall read a text file, and then get the list of all lines in the text file using readlines() function. After that, we use For Loop to traverse these list of strings.

Python Program

#get file object
file1 = open("sample.txt", "r")

#read all lines
lines = file1.readlines()

#traverse through lines one by one
for line in lines:
	print(line.strip())

#close file
file1.close

Output

Hi User!
Welcome to Python Examples.
Continue Exploring.

Example 3: Read File Line by Line using File Object

In our first example, we have read each line of file using an infinite while loop and readline() function. But, you can use For Loop statement on the file object itself to get the next line in the file in each iteration, until the end of file.

Following is the program, demonstrating how we use for-in statement to iterate over lines in the file.

Python Program

#get file object
file1 = open("sample.txt", "r")

#traverse through lines one by one
for line in file1:
	print(line.strip())

#close file
file1.close

Output

Hi User!
Welcome to Python Examples.
Continue Exploring.

Summary

In this tutorial of Python Examples, we learned how to read a text file line by line, with the help of well detailed python example programs.