File Handling in Python

File Handling in Python

  • Python too supports file handling and allows users to handle files i.e., to read and write files, along with many other file handling options, to operate on files.
  • As the part of programming requirement, we have to store our data permanently for future purpose. For this requirement we should go for files.
  • Files are very common permanent storage areas to store our data.
  • File handling is an important part of any web application.
  • Python has several functions for creating, reading, updating, and deleting files.


  • The data stored on a secondary storage media like hard disk or CD is called a file.

Four Important Advantages of Storing Data in a File

  • When the data is stored in a file, it is stored permanently. This means that even though the computer is switched off, the data is not removed form the memory since the file is stored on hard disk or CD. This file data can be utilized later, whenever required.
  • It is possible to update the file data. For example, we can add new data to the existing file, delete unnecessary data form the file and modify the available data of the file. This makes the file more useful.
  • Once the data is stored in a file, the same data can be shared by various programs. For example, once employees data is stored in a file, it can be used in a program to calculate employees' net salaries or in another program to calculate income tax payable by the employees.
  • File are highly useful to store huge amount of data. For example, voters' list or census data.

Types of Files in Python

  1. Text File
  2. Binary Files