Python program to get a specific line from the file

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
  1. Open the file by using open(“readcontent.txt,”r“).
  2. Where the file name is the name of the file.
  3. Read the lines in the file using readlines().
  4. Print the 1st line of the file by using indexing.
  5. 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.

get all the email ids from a text file.

get odd lines from files and append them to separate files.

Leave a Comment