Chapter 11: File Handling in Python – Master Opening, Reading, Writing, CSV, and JSON Files
Vishal Yadav | Course Instructor
Introduction
Imagine building an application that can only process data while it is running. Once the program closes, everything disappears. This is where file handling becomes essential. File handling allows programs to store information permanently, retrieve it later, and exchange data with other applications.
Whether you are saving user information, generating reports, reading configuration files, or working with structured data formats like CSV and JSON, understanding file handling is a fundamental programming skill. In this chapter, you will learn how to open files, read and write data, append information, and work with CSV and JSON files in Python.
Understanding File Handling
File handling refers to the process of creating, opening, reading, writing, modifying, and closing files. Python provides built-in functions that make file operations straightforward and efficient.
The most commonly used function is open(), which creates a file object that can be used for various file operations.
Opening Files
Before performing any operation on a file, it must first be opened. Python uses the open() function for this purpose.
Syntax of open()
The basic syntax is:
open(filename, mode)
Where:
- filename – The name or path of the file.
- mode – Specifies how the file will be accessed.
Common File Modes
- r – Read mode (default).
- w – Write mode (creates a new file or overwrites existing content).
- a – Append mode (adds content without removing existing data).
- x – Create a new file and fail if it already exists.
- rb – Read binary files.
- wb – Write binary files.
Example:
file = open('data.txt', 'r')
After completing file operations, it is important to close the file.
file.close()
Using the with Statement
A better approach is to use the with statement, which automatically closes the file after use.
with open('data.txt', 'r') as file:
content = file.read()
Reading Files
Reading files allows programs to retrieve stored information. Python provides several methods to read file contents.
Reading the Entire File
The read() method reads all contents at once.
with open('data.txt', 'r') as file:
content = file.read()
Reading a Single Line
The readline() method reads one line at a time.
line = file.readline()
Reading All Lines
The readlines() method returns a list containing all lines.
lines = file.readlines()
Looping Through a File
For large files, reading line by line is more memory efficient.
with open('data.txt', 'r') as file:
for line in file:
print(line)
Writing Files
Writing files allows applications to save information for future use.
Using Write Mode
The w mode creates a new file or overwrites an existing one.
with open('output.txt', 'w') as file:
file.write('Hello, World!')
If the file already contains data, it will be completely replaced.
Writing Multiple Lines
The writelines() method can write multiple strings at once.
lines = ['Python
', 'File Handling
', 'Tutorial
']
with open('output.txt', 'w') as file:
file.writelines(lines)
Appending Data
Sometimes you need to add new information without deleting existing content. This is where append mode becomes useful.
Using Append Mode
with open('log.txt', 'a') as file:
file.write('New log entry
')
If the file does not exist, Python automatically creates it.
Practical Applications of Appending
- Maintaining application logs.
- Recording user activity.
- Saving transaction histories.
- Adding new records to datasets.
Working with CSV Files
CSV (Comma-Separated Values) files are widely used for storing tabular data such as spreadsheets and database exports.
What is a CSV File?
A CSV file stores data in rows and columns, with values separated by commas.
Name,Age,City
John,25,New York
Sarah,30,London
Reading CSV Files
import csv
with open('students.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
Writing CSV Files
import csv
with open('students.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Name', 'Age', 'City'])
writer.writerow(['John', 25, 'New York'])
Benefits of CSV Files
- Easy to read and edit.
- Compatible with spreadsheet software.
- Lightweight and portable.
- Commonly used for data exchange.
Working with JSON Files
JSON (JavaScript Object Notation) is one of the most popular formats for storing and exchanging structured data.
Why JSON is Important
JSON is human-readable, lightweight, and widely used in APIs, web applications, and configuration files.
Example JSON Data:
{
"name": "John",
"age": 25,
"city": "New York"
}
Reading JSON Files
import json
with open('data.json', 'r') as file:
data = json.load(file)
print(data)
Writing JSON Files
import json
student = {
'name': 'John',
'age': 25,
'city': 'New York'
}
with open('data.json', 'w') as file:
json.dump(student, file, indent=4)
Advantages of JSON
- Easy for humans to read.
- Easy for machines to parse.
- Supports nested structures.
- Widely used in APIs and web development.
Best Practices for File Handling
- Always use the with statement when working with files.
- Handle exceptions using try-except blocks.
- Use appropriate file modes.
- Validate file paths before opening files.
- Close files properly if not using with.
- Use CSV for tabular data and JSON for structured data.
Conclusion
File handling is one of the most important skills for any Python developer. It allows programs to store, retrieve, and manage data efficiently. In this chapter, you learned how to open files, read content, write new information, append data, and work with popular file formats such as CSV and JSON.
Mastering these concepts will help you build applications that can save user data, generate reports, process datasets, and interact with modern web services. As you continue your Python journey, file handling will become a critical tool in almost every project you create.
Vishal Yadav
A specialist dedicated to publishing high-quality, readable insights on technology, leadership, and digital growth.