readlines() function in file handling in python by R4R Team

- readlines() is a python function used in file handling which read all line of the file and make a list of each line.

Syntax-

f=open('myfile.txt','r')
f.readlines()

-It return the list that contain the each line of the file.

Example-

file data is -

first
second
third

then readlines() return the ['first','second','third']


program-

#write data in file in multiple lines
f=open("myfile.txt",'w')
f.write("Firstn")
f.write("secondn")
f.write("Thirdn")
f.write("Fourthn")
f.write("Fiven")
f.write("Sixn")
f.close()

#read file line by line
f=open('myfile.txt','r')
print(f.readlines())
f.close()


output-

['Firstn', 'secondn', 'Thirdn', 'Fourthn', 'Fiven', 'Sixn']


-In this program, we write the data in file in mulitple lines then readlines() take each line and make the list and return.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!