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

- writelines() is a python file function which are used to insert a data into the file in muliple lines.

Syntax-

f=open('myfile.txt','w')
f.writelines(string)


program-

#write data into file in multiple lines
f=open('myfile.txt','w')
f.writelines("first linensecond linenthird linenfourth linenfifth line")
f.close()

#read the file
f=open('myfile.txt','r')
s=f.read()
print(s)
f.close()


output-

first line
second line
third line
fourth line
fifth line


-In this program, we insert the data by writelines() function and string are pass that contain line break(n) so writelines() function insert according to the 'n' into multiple lines, and by using read() function we read the file.




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!