write() and read() in file handling in python by R4R Team

read()-
- read() is a python file function which are used to read the existing file.

Syntax-

f.read()

-Here 'f' is the object which points the existing file.

write()-
- write() is a python file function which are used to write in the file.
- If the file is not exist then write() function create new file.

Syntax-

f.write(string)

-Here string is the content which we want to insert into the file.


program-


#opne file in write mode
f=open("myfile.txt",'w')
f.write("Data insert into the file")
f.close()

#open file in read mode
f=open("myfile.txt",'r')
data=f.read()
print(data)
print(type(data))
f.close()


output-

Data insert into the file


-In this program, initially we open the file named as 'myfile.txt' and 'txt' is shows that file is of type 'text',
-then by f.write("Data insert into the file") we write this string into the file.
-then by f.read() we get the all text of the file then we print that.




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!