Need of file handling in programming in python

harlearncs 11 views 13 slides Oct 18, 2025
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

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...


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:

THANK YOU