File Handling

File handling refers to the management of files in programming. The concept of file handling allows us to open, close, read, write, append, and copy files. Python treats different types of files, whether in binary or text format. To implement file handling in Python, the syntax used is: file = open(‘filename’, ‘mode’).

Python provides three types of modes for opening files:

1
2
3
4
"r", for Read.
"w", for Write.
"a", for Addition.
"r+", for Read & Write

And don’t forget, once we have finished working with a file, we must call the method to close it. The method to close a file is called using file.close().

MODULE

Download Module 9