Decode School

Decode School - The School for Problem Solvers
Welcome to My Blog.
Need Assignment / Project Guidance (or) Training in Python?

Read file line by line in python

Python program to read a file line by line

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.