File handling in Python involves creating, reading, updating, and deleting files. Python provides several functions to handle files, which gives the flexibility to do different operations on a file.
1. Opening a file: Python has a built-in function `open()` to open a file. This function takes two parameters, the filename and the mode (read, write, append, etc.) in which the file is opened. For example:
```
file = open(“test.txt”) # open file in current directory
```
or
```
file = open(“C:/test.txt”) # specifying full path
```
1. Modes of opening a file: The mode argument is optional and ‘r’ will be assumed if it’s omitted. The modes are – ‘r’ (read), ‘w’ (write), ‘a’ (append), ‘r+’ (read and write), ‘b’ (binary mode), ‘t’ (text mode).
1. Reading a file: `read()` method is used to read the whole file, `readline()` method is used to read a line of the file, `readlines()` method is used to read all lines in the file.
1. Writing to a file: `write()` method is used to write a string to a file. If the file existed, it will be truncated to zero length and overwritten. If the file does not exist it will be created.
1. Appending to a file: ‘a’ or ‘a+’ mode can be used to append to the file. The `write()` function is then used to add to the file.
1. Closing a file: It is a good practice to always close the file when you are done with it. This is achieved with `close()` method.
1. Using `with` statement: It is good practice to use the with keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way.
Here is an example demonstrating file handling:
```
with open(‘test.txt’, ‘r’) as f:
print(f.read()) # prints the content of the file
```