File Handling Basics
M3-R5.1 · Chapter 7: File Processing · 2 min read
What is File ?
A file is a named location on disk used to store data permanently. Unlike variables (which store data in RAM and lose data when the program ends), files provide persistent storage that survives program termination.
▸ 1.1 Why Do We Need Files?
- To store large amounts of data permanently on secondary storage (HDD/SSD).
- To share data between different programs and users.
- To process data that is too large to fit entirely in memory (RAM).
- To maintain records, logs, configuration, or results for future use.
▸ 1.2 Types of Files in Python
Key Difference
Text files use \n as line separator, and Python converts it based on the OS.
Binary files have no special conversion — data is stored byte-for-byte exactly as is.
What is File Handling ?
File handling in Python refers to the built-in process of creating, opening, reading, updating, and deleting files on a system directly through code.
It bridges the gap between temporary variables in memory and persistent storage on a hard drive, ensuring data survives even after your script stops running.
The Core File Life Cycle
Every file operation in Python follows three sequential steps:
- Opening the file to create a resource connection link.
- Processing the file by reading data from it or writing data to it.
- Closing the file to flush changes and free system memory resources.
▸ 1.3 File Access Modes Overview
Python provides different modes to control how a file is opened and accessed: