In this python file program, we will get a specific line from the file. Let’s consider we have readcontent.txt file with the below content. We will get a specific line from the file with the help of the below-given steps.
# 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(“readcontent.txt,”r“).
- Where the file name is the name of the file.
- Read the lines in the file using readlines().
- Print the 1st line of the file by using indexing.
- Close the file.
# Open the file
file = open("readcontent.txt","r")
# Read lines of the file
data = file.readlines()
# Print 1st line
print("1st line")
print(data[0])
# Close the file
file.close()
Output :
1st line
Line1 : This is India.