Read file line by line in python

Python program to read a file line by line

Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

#write your code here

Program or Solution

				
			
					
file=open("sample.txt",'r')
content = file.readline()
while(content):
    print(content)
    content=file.readline()
file.close()

			
				
			

Program Explanation

open the file in read mode read line by line using readline() method.

Read a line and check whether there is content in the line, then print it and read the next line.

Repeat it until a line has no content.

close() the file.

Comments


Related Programs