Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Input-Output in python
  5. Reading and Writing files

Reading and Writing files

File handling is an important part of any web application. These include creating, writing, reading and deleting a file.

In python, we don’t have to import any library or package for creating a file. We have inbuilt function to do this.

The key function for working with files in python is the open() funtion.


object=open("filename", "mode")

There are different modes for opening file:-

  • Read mode – r- This mode is used to read the file
  • Write mode- w- This mode is used to write the file
  • Read and write mode- w+ – This mode is used to both read and write a file.
  • Append mode- a – This mode is used to append the file

We can open file by using open() function and close file using close() function.



We can read a file by using read mode. We have a function read which helps in reading a file.


file = open("filename") 

This is how we can readd a file.

We also have a function write() which helps in writing in a file.


file = open("filename", 'w') 
file.write("String you want to add in the file") 

This is how we can read and write in a file in python.

Was this article helpful to you? Yes No

How can we help?