append 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=open('file.txt','r')
f.read()

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

Append in file-

Syntax-

f=open('file.txt','a')
f.write(string)

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


program-



#opne file in append mode
f=open("file.txt",'a')
f.write("First time insertionn")
f.close()

#open file in read mode
print("File open first time")
f=open("file.txt",'r')
print(f.read())
f.close()

#again append into this file
f=open('file.txt','a')
f.write("Second time insertion")
f.close()

#open file in read mode
print("File open again")
f=open("file.txt",'r')
print(f.read())
f.close()


output-


First time insertion
First time insertion

Second time insertion
First time insertion
Second time insertion


-In this program, we open the file in append mode and append the string in file by write() function
-and by read() function, we read the content of 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!