Curriculum
File Handling is one of the most important concepts in Python programming because it allows programs to create, read, write, update, and manage files stored on a computer. In Data Analytics, Data Science, Machine Learning, Business Analytics, and Software Development, File Handling is widely used for processing datasets, generating reports, storing results, and automating business workflows.
Most real-world analytical projects involve working with files such as:
Understanding File Handling is essential because data analysts frequently import data from files, clean it, process it, and export analytical results back into files.
Organizations use File Handling for:
Mastering File Handling is a key skill for Python developers and Data Analysts.
File Handling refers to the process of interacting with files through a program.
Python allows users to:
Example:
file = open("data.txt", "r")
This statement opens a file for reading.
File Handling provides several benefits.
Benefits:
Without File Handling, data would be lost when a program closes.
Python commonly works with two categories of files.
Store readable text.
Examples:
Store data in binary format.
Examples:
Data Analysts primarily work with text files.
Python uses the open() function.
open(file_name, mode)
Example:
file = open("data.txt", "r")
Parameters:
The mode determines how the file is accessed.
Python supports several file modes.
| Mode | Description |
|---|---|
| r | Read |
| w | Write |
| a | Append |
| x | Create |
| rb | Read Binary |
| wb | Write Binary |
Understanding file modes is essential for safe file operations.
Read mode opens a file for reading.
Example:
file = open("data.txt", "r")
print(file.read())
Applications:
Data extraction.
Report analysis.
Write mode creates a new file or overwrites an existing file.
Example:
file = open("data.txt", "w")
file.write("Welcome to Python")
Applications:
Report generation.
Data export.
Append mode adds content to an existing file.
Example:
file = open("data.txt", "a")
file.write("\nNew Record")
Benefits:
Preserves existing data.
Creates a new file.
Example:
file = open("new_file.txt", "x")
Applications:
New project creation.
Files should always be closed after use.
Example:
file = open("data.txt", "r")
print(file.read())
file.close()
Benefits:
The recommended approach is using with.
Example:
with open("data.txt", "r") as file:
print(file.read())
Benefits:
Automatic file closing.
Cleaner code.
Professional programming practice.
Example:
with open("data.txt", "r") as file:
content = file.read()
print(content)
Applications:
Data loading.
Example:
with open("data.txt", "r") as file:
print(file.readline())
Applications:
Large file processing.
Example:
with open("data.txt", "r") as file:
lines = file.readlines()
print(lines)
Output:
['Line 1', 'Line 2']
Applications:
Dataset processing.
Example:
with open("report.txt", "w") as file:
file.write("Monthly Sales Report")
Applications:
Business reporting.
Example:
with open("report.txt", "w") as file:
file.write("Sales Report\n")
file.write("Revenue: 500000")
Applications:
Automated report generation.
Example:
with open("report.txt", "a") as file:
file.write("\nProfit: 200000")
Benefits:
Historical data tracking.
Python provides file validation methods.
Example:
import os
print(
os.path.exists("data.txt")
)
Output:
True
Applications:
Error prevention.
Example:
import os
os.remove("data.txt")
Applications:
Temporary file cleanup.
CSV files are widely used in Data Analytics.
Example:
import csv
with open(
"sales.csv",
"r"
) as file:
reader = csv.reader(file)
for row in reader:
print(row)
Applications:
Dataset processing.
Example:
import csv
with open(
"sales.csv",
"w",
newline=""
) as file:
writer = csv.writer(file)
writer.writerow(
["Product", "Revenue"]
)
writer.writerow(
["Laptop", 50000]
)
Applications:
Report exports.
JSON is widely used for structured data.
Example:
import json
data = {
"name": "Rahul",
"age": 22
}
with open(
"student.json",
"w"
) as file:
json.dump(
data,
file
)
Applications:
Data exchange.
API integration.
Example:
import json
with open(
"student.json",
"r"
) as file:
data = json.load(file)
print(data)
Applications:
Data retrieval.
Data Analysts frequently use File Handling for:
Example:
with open(
"sales_data.csv",
"r"
) as file:
print(file.read())
Applications:
Business reporting.
Machine Learning projects use files for:
Benefits:
Persistent storage.
Business Analysts use files for:
Benefits:
Improved decision-making.
Example:
sales = [
10000,
15000,
20000
]
with open(
"sales_report.txt",
"w"
) as file:
for sale in sales:
file.write(
str(sale) + "\n"
)
Result:
10000
15000
20000
Applications:
Automated reporting.
Can cause resource leaks.
Produces FileNotFoundError.
May overwrite important data.
Can consume excessive memory.
Avoiding these mistakes improves application reliability.
Ensure automatic closing.
Prevent runtime errors.
Protect data.
Improve reliability.
Improve maintainability.
These practices support professional programming.
Benefits include:
File Handling is a critical skill for Data Analytics and Software Development.
After completing this lesson, you will be able to:
File Handling allows programs to create, read, write, update, and manage files.
It enables permanent data storage and report generation.
The open() function opens files for processing.
Using the with statement.
Read (r), Write (w), Append (a), and Create (x).
They are commonly used for storing and sharing datasets.
It stores structured data and supports API communication.
It enables data import, processing, storage, and report generation.
Want to master Python, SQL, Power BI, and Data Analytics?
WhatsApp us