Basic Python - File handling


NaN years ago

In a real-world application, most of the process needs to read data from files and write data into files, Python provides built-in support for file handling and allows users to handle files, along with many other file handling options like mode and buffering, and encoding.

file_object = open(filename, mode, buffering, encoding) 

filename: It is the name of the file or relative path of the file.

mode: It tells the program in which mode the file has to be open, type of modes are:

  • r : for reading.
  • w : for writing.
  • a : for appending.
  • r+ : for both reading and writing

buffering: Here, if the value is set to zero (0), no buffering will occur while accessing a file; if the value is set to top one (1), line buffering will be performed while accessing a file.

Working of read mode

file = open("file.text", "r") 
print (

Working of write mode

file = open('geek.txt','w')
file.write("This is the write command")
file.write("It allows us to write in a particular file")

Working of append mode

file = open('geek.txt','a')
file.write("This will add this line")

Learn more about file handling and its options using resources

approvedApproved by experts
Advance Topics

Neuton's Mission

We are a team of young and enthusiastic people who are passionate about education and want to help students to learn the skills they need to succeed in life. If you want to support us, please join our community.