Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Input-Output in python
  5. Opening and closing file

Opening and closing file

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.

syntax:-

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.

syntax:-

file.close()

This is all about opening and closing a file in python.

Was this article helpful to you? Yes No

How can we help?