KEMBAR78
Need of file handling in programming in python | PDF
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

Need of file handling in programming in python

  • 1.
    NEED OF FILEHANDLING PROGRAMMING IN PYTHON File Handling
  • 2.
    WHAT IS FILE? Filesare 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.
  • 3.
    WHY DO WENEED 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
  • 4.
    VARIOUS OPERATIONS CARRIEDOUT ON A FILE ARE Creating a file Opening a file Reading from a file Writing to a file Closing a fi le
  • 5.
    CREATING A FILE Toread 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:
  • 6.
    OPENING A FILE Wehave 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])
  • 7.
    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
  • 8.
    OPENING A FILEATTRIBUTES
  • 9.
    CLOSING A FILE Wecan 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()
  • 10.
  • 11.
  • 12.
    WRITING A FILE InPython, 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:
  • 13.