Articles → Python → Reading a text file in python

Reading a text file in python






Example





Click to Enlarge

myfile = open("test.txt","r")
print(myfile.read())







Click to Enlarge


Specify the number of characters in read function




myfile = open("test.txt","r")
print(myfile.read(10))





Click to Enlarge


Readline function




myfile = open("test.txt","r")
print(myfile.readline())





Click to Enlarge



myfile = open("test.txt","r")
print(myfile.readline(10))  



Click to Enlarge


Read line by line




myfile = open("test.txt","r")
for line in myfile:
   print(line)





Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Monday, March 04, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250