File handling is essential in computer programming because it enables the storage, retrieval, and management of data permanently beyond a program’s runtime. In simple terms, while variables and data structures store temporary data during execution, file handling ensures that information persists e...
File handling is essential in computer programming because it enables the storage, retrieval, and management of data permanently beyond a program’s runtime. In simple terms, while variables and data structures store temporary data during execution, file handling ensures that information persists even after the program ends .Meaning and Concept of File HandlingFile handling refers to performing operations such as creation, reading, updating, writing, and deletion on files through a program. Files serve as non-volatile storage mediums on hard drives or SSDs, enabling programs to interact with external data sources efficiently . There are usually two types of files:Text files, which store human-readable ASCII data (e.g., .txt or .csv).Binary files, which store data in binary form for compactness and faster I/O operations .Need for File Handling in ProgrammingThe primary need for file handling arises from the limitation of memory-based data storage. When a program terminates, all data held in variables is lost. Files allow programmers to preserve data permanently, manipulate large datasets efficiently, and share information between applications without manually re-entering data .Key Reasons for File HandlingData Persistence and StorageFiles allow permanent storage of data, enabling access and reuse of information across multiple runs of a program .For instance, a banking application must store user transactions or account data even after the system shuts down.Data Access and RetrievalUsing file reading operations, programs can extract relevant data from large files efficiently without excessive memory consumption .This is crucial in big data processing, where datasets often exceed available system memory.Data Sharing and PortabilityFiles make it possible to transfer data seamlessly between computers or programs. For example, exporting reports as .csv or .json files facilitates cross-platform compatibility .Automation and ConfigurationModern applications use configuration files (e.g., .ini, .yaml, .json) to store reusable settings, removing the need for manual reconfiguration each time .Security and BackupFile handling supports secure data management by enforcing permissions and controls through file handles. Backup copies of files protect against system failures or data loss .Performance and OptimizationWith buffering and file handles, programs can reduce system overhead by managing data flow between memory and disk efficiently, especially when dealing with large files or simultaneous processes .Practical Examples of File Handling Logging applications – To record events or errors continuously.Database management – Where data must be stored, indexed, and retrieved efficiently.User input/output – Through files to process form data, settings, or reports.Scientific simulations – Where large numeric results must be stored for analysis.Backup utilities – For replicating and saving important documents systematically. File handling modes read, write, append
Size: 3.44 MB
Language: en
Added: Oct 18, 2025
Slides: 13 pages
Slide Content
NEED OF FILE HANDLING
PROGRAMMING IN PYTHON File
Handling
WHAT IS FILE?
Files are named locations on disk to store related information. They are used to
permanently store data in a non-volatile memory (e.g. hard disk).
Since Random Access Memory (RAM) is volatile (which loses its data when the
computer is turned off), we use files for future use of the data by permanently
storing them.
Data is stored in files in two ways:
1.Text Format- In text format data is stored as a line of character with each line
terminated by a new line character (\n). Text files are in human readable form.
2.Binary Format-In binary format, data is stored on the disk same way as it is
represented in the computer memory. Binary files are not in human readable form
they and can be created and read by a specific program written for them.
WHY DO WE NEED FILE HANDLING?
To store data permanently, even after
the program ends.
To access external files like .txt, .csv, .json,
etc.
To process large files efficiently without
using much memory
VARIOUS OPERATIONS CARRIED OUT ON A FILE ARE
Creating a file
Opening a file
Reading from a file
Writing to a file
Closing a fi le
CREATING A FILE
To read data from a
file or to write data
to a file, a user
needs to use the
open function to first
create a file object
Example:
OPENING A FILE
We have to open a file using built-in function open()
This function returns a file object, also called a handle, it is used to read
or modify the file
We can specify the mode while opening a file.
r-Read
w-Write
a - Append
Syntax:
file object= open (filename [,accessmode] [, buffering])
OPENING A FILE
filename: name of the file that we want to access
accessmode:read, write, append etc.
buffering: 0-no buffering,1- line buffering
Example:
f=open ("abc.txt","r') open file in current directory
OPENING A FILE ATTRIBUTES
CLOSING A FILE
We can close a file in Python using
the close() method. Closing a file is
an essential step in file handling to
ensure that all resources used by
the file are properly released. It is
important to close files after
operations are completed to
prevent data loss and free up
system resources.
Example:
Syntax:
Fileobject.close()
READING A FILE
READING A FILE
Example:Notepad:
Output:
My name
is Haridharani
WRITING A FILE
In Python, writing to a file is done
using the mode "w". This creates a
new file if it doesn’t exist, or
overwrites the existing file if it does.
The write() method is used to add
content. After writing, make sure to
close the file.
Example: