We will read the text file in read (r) mode in this Python program. Let’s consider we have readcontent.txt file with the below content. We will read this file content in read mode with the help of the steps below.
readcontent.txt Line1 : This is India. Line2 : This is America. Line3 : This is Canada. Line4 : This is Australia
Steps to solve the program
- Open the file by using open(“filename”,”r”) function.
- Where filename is the name of the file and r is read mode.
- Read file content with read() method and store it in the data variable.
- Print the data.
- Close the file.
# open file in read mode file = open('readcontent.txt', 'r') # read content of the file data = file.read() # print file data print(data) # close the opened file file.close()
Output:
Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia