File Handling
• Python 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.
• Python treats files differently as text or binary.
File Opening
• Before performing any operation on the file like reading or writing, first, we have to open that file.
Where the following mode is supported:
1. r: open an existing file for a read operation.
2. w: open an existing file for a write operation. If the file already contains some data then it will be overridden.
3. a: open an existing file for append operation. It won’t override existing data.
4. r+: To read and write data into the file. The previous data in the file will be overridden.
5. w+: To write and read data. It will override existing data.
6. a+: To append and read data from the file. It won’t override existing data.
File Opening
The open command will open the file in the read mode and the for loop will print each line present in
the file.
File Opening
If you need to extract a string that contains all characters in the file then we can use [Link]()
File Opening
To read a file for certain number of characters.
The interpreter will read the first five characters of stored data and return it as a
string:
File Creation
Appending File
Splitting File