Read a file line by line and print it

file=open('demo.txt','r') # open file in read mode file should be there in the same folder,
#other wise you have to enter full path of file location ,for example c:\programs\demo.txt
 
for line in file:
   print(line) #print each line in for loop
 
Explanation: In this program first we are opening a file using open function in python. open function can take many arguments. for details of open function read file handling in python article. here we are are passing name of the file and mode in which we want to Access the file. r refers to read mode in function. we just want to read the file line by line.
 
file must exist in the current directory or the path given by you, other wise program will throw error in compiler. in next line of program we are running a for loop so that we can read each line in the file until EOF character. EOF stands for end of file. each line in file is separated by '\n' character in file which operating system puts automatically in file after each line. we are printing each line in the loop.