We have learned how to create and use variables in Python. However, this is a temporary arrangement; they cease to exist once the program finishes executing. Python provides the capability to store data permanently, allowing it to be used even after the program has terminated. This is known as 'file handling'.
For example: Storing Records, Read file data, Store notes etc.
What is File Handling in Python?
"File Handling" is a concept used to read, write and modify file in Python language. With this, we can:
- Create a File
- Read a File
- Write data to a File
- Update a File
There are several methods and fuctions avaliable in file handling like: open(), write(), read() etc.
File Mode
Python file handling has multiple modes that serves the specific purpose.
| Mode | Meaning | Behavior |
|---|---|---|
"r" | Read | Open a file only for reading. File must exist. |
"w" | Write | Open a file for writing. Creates a new file or overwrites an existing file. |
"a" | Append | Open a file to add data at the end. Existing content is preserved. |
"x" | Exclusive creation | Create a new file. Fails if the file already exists. |
"rb" | Read Binary | Read a file in binary mode (images, videos). |
"wb" | Write Binary | Write a file in binary mode. |
"r+" | Read & Write | Open a file for reading and writing. File must exist. |
"w+" | Write & Read | Open a file for reading and writing. Overwrites existing file. |
"a+" | Append & Read | Open a file for reading and appending. Adds new content at the end. |
Creating a New File
Create new file using python code.
Example
file = open("newfile.text", "x")
file.close()Opening a File
To open any file, we can use open() function.
Note: "r" mode does not create a file.
Syntax
file = open("filename.txt", "mode")
Example
file = open("Python.text", "r") // Python.txt - file name, r - read mode
print(file.read())
Read a File
To read any file, we can use read() function.
Example
file = open("Python.text", "r")
print(file.read())
file.close()Read one line
Example
file = open("Python.text", "r")
print(file.readline())
file.close()Read all lines
Example
file = open("Python.text", "r")
print(file.readlines())
file.close()Writing a File
The "w" mode is used to overwrite an existing file; that is, no old data remains in it. If the file does not exist, it will create a new file.
Example
file = open("note.txt", "w")
file.write("Python Programming")
file.close()Appeding Data
With the help of the "a" mode, we can add new lines without deleting the existing data. If the file does not exist, it also creates it.
Example
file = open("note.text", "a")
file.write("\nLearn from Scratch")
file.closeClosing File
Always close files after using them.
file.close()This saves resources.
Use "With" statment
This statement helps us to automatically close the file after execution
Example
with open("note.text", "r") as file:
print(file.read())Save User Input
We can also save the user input direct in our text file.
Example
name = input("Enter your name: ")
with open("note.text", "w")
file.write("name +\n")
Practice Excercise
Task 1: Create a file and write your name into it.
Task 2: Read data from a file.
Task 3: Append your city name to an existing file.
Task 4: Store 5 student names in a file.
Conclusion
This covers the basics of file handling at an introductory level. Practice these exercises and study this chapter in depth, as the concept of file handling enables applications to store data permanently. Next Chapter: OOPs in Python
Happy learning!
