Python: How to Read a File in Reading Mode

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

  1. Open the file by using open(“filename”,”r”) function.
  2. Where filename is the name of the file and r is read mode.
  3. Read file content with read() method and store it in the data variable.
  4. Print the data.
  5. 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


Leave a Comment