You are here: Home / Topics / Append and read in file handling in python

Append and read in file handling in python

Filed under: Python on 2022-08-04 20:13:26

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.

About Author:
V
Vaibhav Shukla     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.