File Handling in Python
File Handling in Python
A School Project
Prepared by: [Your Name]
Class: [Your Class]
Subject: Computer Science
Date: [Submission Date]
Introduction
File handling is one of the most essential features in Python. It enables users to handle files
such as reading, writing, and appending data to them. This capability allows programs to
permanently store and retrieve data, essential for applications requiring data persistence.
Importance of File Handling
Without file handling, data in Python would be lost once the program ends. File handling
enables reading from and writing to text, CSV, JSON, and binary files, allowing for long-term
data management.
Types of Files
1. Text Files (.txt)
2. Binary Files (.bin)
3. CSV Files (.csv)
4. JSON Files (.json)
Each type of file serves different purposes in real-life applications.
File Modes in Python
'r' - Read
'w' - Write
'a' - Append
'x' - Create
'r+' - Read and Write
Adding 'b' to any mode enables binary mode (e.g., 'rb', 'wb').
Opening Files
Python uses the built-in `open()` function to open files.
Syntax: `open(filename, mode)`
Example: `file = open('data.txt', 'r')`
Reading Files
`read()` - Reads the entire content
`readline()` - Reads a single line
`readlines()` - Reads all lines into a list
Writing to Files
`write()` - Writes a string to the file
`writelines()` - Writes a list of strings to the file
Appending Data
Appending allows you to add data to the end of the file without modifying the existing
content.
Example: `file = open('data.txt', 'a')`
Closing Files
`close()` is used to close a file and free up system resources.
Always close the file after use.
Using with Statement
`with open('data.txt', 'r') as file:`
This method is preferred as it handles closing files automatically, even if exceptions occur.
Exception Handling in File Operations
Use try-except blocks to manage errors such as FileNotFoundError or IOError.
Example:
try:
file = open('data.txt')
except FileNotFoundError:
print('File not found')
Working with Binary Files
Binary files store data in bytes and are used for non-text files such as images and
executables.
Use mode 'rb' or 'wb' for binary files.
Working with CSV Files
Use the csv module in Python:
import csv
with open('data.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
Working with JSON Files
Use the json module to read/write JSON data:
import json
with open('data.json') as file:
data = json.load(file)
Practical Application Examples
1. Log systems
2. Student data management
3. Inventory management systems
4. Automated report generation
Mini Project Example
file = open('notes.txt', 'w')
file.write('Python File Handling Example')
file.close()
file = open('notes.txt', 'r')
print(file.read())
file.close()
Conclusion
File handling is an indispensable tool in Python programming. Mastering it enables effective
data storage, manipulation, and retrieval, which are critical in real-world applications
across fields.
Acknowledgements
I would like to express my sincere gratitude to my teacher, classmates, and parents for their
constant support and encouragement in completing this project successfully.