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.