- Learning Objectives
- Prerequisites
- Introduction
- What is File Handling?
- Why is File Handling Required?
- Types of Files
- File Operations
- File Handling Life Cycle
- Internal Working
- Summary
After completing this chapter, you will be able to:
- Understand File Handling.
- Learn why File Handling is required.
- Understand different file types.
- Learn the basic file operations.
- Understand the File Handling life cycle.
- Prepare for interview questions related to File Handling.
Before learning this chapter, you should know:
- Variables
- Strings
- Functions
- Lists
- Exception Handling
A program stores data in memory while it is running.
However,
when the program terminates,
all data stored in memory is lost.
Sometimes,
we need to store information permanently.
Examples include:
- Student records
- Employee details
- Banking information
- User credentials
- Application logs
- Configuration files
Python provides File Handling to store and retrieve data from files.
File Handling is the process of creating, opening, reading, writing, updating, and deleting files using a programming language.
File Handling allows programs to store data permanently inside files.
Without File Handling,
every time the program closes,
all information stored in memory disappears.
Using files allows us to:
- Store data permanently.
- Retrieve data whenever required.
- Share information between programs.
- Maintain application records.
- Save reports and logs.
Stores
- Customer Details
- Transactions
- Account Information
Stores
- Student Records
- Attendance
- Marks
- Fee Details
Stores
- Patient Details
- Medical Reports
- Billing Information
Stores
- Orders
- Product Information
- Customer Details
- Payment Records
Python mainly works with two categories of files.
These files store human-readable data.
Examples
.txt
.csv
.json
.xml
.html
.py
.md
Example
Name : John
Age : 25
Binary files store data in binary format.
These files are generally not human-readable.
Examples
.jpg
.png
.mp3
.mp4
.pdf
.exe
.docx
.xlsx
Python supports several operations on files.
Creates a new file if it does not already exist.
Opens an existing file for processing.
Retrieves data from a file.
Writes new data into a file.
Adds new data to the end of an existing file.
Modifies existing file content.
Releases system resources associated with the file.
Removes a file from the storage device.
Create File
↓
Open File
↓
Read / Write / Append
↓
Save Changes
↓
Close File
↓
File Stored Permanently
- Permanent storage.
- Easy retrieval.
- Data sharing.
- Better application management.
- Supports large amounts of data.
- Useful for backups.
- Improves data persistence.
- Reading and writing files is generally slower than accessing data in memory.
- Files can become corrupted.
- Improper handling may cause data loss.
- Permissions may restrict file access.
- Always close files after use.
- Use the
withstatement whenever possible. - Handle file-related exceptions.
- Use meaningful file names.
- Store files in organized directories.
- Validate file paths before accessing files.
Forgetting to close files.
Opening files in the wrong mode.
Ignoring exceptions such as
FileNotFoundErrorAttempting to read from a file opened in write-only mode.
Python Program
↓
open()
↓
Operating System
↓
Storage Device
↓
File
↓
Read / Write / Append
↓
close()
Program Starts
↓
Open File
↓
Operating System Locates File
↓
File Handle Created
↓
Perform File Operation
↓
Save Changes
↓
Close File
↓
Program Ends
Instead of manually closing files,
Python provides the with statement.
Benefits:
- Automatically closes files.
- Cleaner code.
- Safer resource management.
- Handles exceptions more effectively.
Program Starts
↓
Open File
↓
Read or Write Data
↓
Save Changes
↓
Close File
↓
Program Ends
Python Program
│
├── RAM
│ │
│ └── File Object
│
└── Storage Device
│
└── data.txt
File Handling is the process of creating, reading, writing, updating, and deleting files using a programming language.
It allows programs to store data permanently instead of keeping it only in memory.
- Text Files
- Binary Files
RAM stores temporary data, while files provide permanent storage on a storage device.
.txt.csv.json.xml.html.md
.jpg.png.pdf.docx.xlsx.exe
- Create
- Open
- Read
- Write
- Append
- Update
- Close
- Delete
Closing a file releases system resources and ensures that all pending data is written properly.
The with statement.
FileNotFoundError- Explain the File Handling life cycle.
- Differentiate text files and binary files.
- List all common file operations.
- Explain why the
withstatement is preferred. - Explain the advantages of File Handling.
- File Handling → Permanent storage.
- Text File → Human-readable.
- Binary File → Machine-readable.
- Common Operations → Open, Read, Write, Append, Close.
with→ Automatically closes files.FileNotFoundError→ File does not exist.
In this chapter, you learned:
- Introduction to File Handling
- Need for File Handling
- Types of Files
- File Operations
- File Handling Life Cycle
- Advantages
- Limitations
- Best Practices
- Common Mistakes
- Interview Questions
- Practice Questions
- Quick Revision