Grade 12 Exam  >  Grade 12 Notes  >  Computer Science for Grade 12  >  Detailed Notes: File Handling

Detailed Notes: File Handling | Computer Science for Grade 12 PDF Download

Download, print and study this document offline
Please wait while the PDF view is loading
 Page 1


Need for 
a data file
• To Store data in organized manner
• To store data permanently
• To access data faster
• To Search data faster
• To easily modify data later on
Page 2


Need for 
a data file
• To Store data in organized manner
• To store data permanently
• To access data faster
• To Search data faster
• To easily modify data later on
File Handling
A file is a sequence of bytes on the disk/permanent storage where a group
of related data is stored. File is created for permanent storage of data.
In programming, Sometimes, it is not enough to only display the data on the
console. Those data are to be retrieved later on,then the concept of file
handling comes. It is impossible to recover the programmatically generated
data again and again. However, if we need to do so, we may store it onto
the file system which is not volatile and can be accessed every time. Here,
comes the need of file handling in Python.
File handling in Python enables us to create, update, read, and delete the
files stored on the file system through our python program. The following
operations can be performed on a file.
In Python, File Handling consists of following three steps:
? Open the file.
? Process file i.e perform read or write operation.
? Close the file.
Page 3


Need for 
a data file
• To Store data in organized manner
• To store data permanently
• To access data faster
• To Search data faster
• To easily modify data later on
File Handling
A file is a sequence of bytes on the disk/permanent storage where a group
of related data is stored. File is created for permanent storage of data.
In programming, Sometimes, it is not enough to only display the data on the
console. Those data are to be retrieved later on,then the concept of file
handling comes. It is impossible to recover the programmatically generated
data again and again. However, if we need to do so, we may store it onto
the file system which is not volatile and can be accessed every time. Here,
comes the need of file handling in Python.
File handling in Python enables us to create, update, read, and delete the
files stored on the file system through our python program. The following
operations can be performed on a file.
In Python, File Handling consists of following three steps:
? Open the file.
? Process file i.e perform read or write operation.
? Close the file.
File Handling
Types of File
There are two types of files:
Text Files- A file whose contents can be viewed using a text editor is called a text file. A text
file is simply a sequence of ASCII or Unicode characters. Python programs, contents written
in text editors are some of the example of text files.e.g. .txt,.rtf,.csv etc.
Binary Files-A binary file stores the data in the same way as as stored in the memory. The
.exe files,mp3 file, image files, word documents are some of the examples of binary files.we
can’t read a binary file using a text editor.e.g. .bmp,.cdr etc.
Text File Binary File
Its Bits represent character. Its Bits represent a custom data.
Less prone to get corrupt as change reflects as 
soon as made and can be undone.
Can easily get corrupted, corrupt on even single
bit change
Store only plain text in a file.
Can store different types of data (audio, 
text,image) in a single file.
Widely used file format and can be opened in any 
text editor.
Developed for an application and can be opened 
in that application only.
Mostly .txt,.rtf are used as extensions to text files. Can have any application defined extension.
Page 4


Need for 
a data file
• To Store data in organized manner
• To store data permanently
• To access data faster
• To Search data faster
• To easily modify data later on
File Handling
A file is a sequence of bytes on the disk/permanent storage where a group
of related data is stored. File is created for permanent storage of data.
In programming, Sometimes, it is not enough to only display the data on the
console. Those data are to be retrieved later on,then the concept of file
handling comes. It is impossible to recover the programmatically generated
data again and again. However, if we need to do so, we may store it onto
the file system which is not volatile and can be accessed every time. Here,
comes the need of file handling in Python.
File handling in Python enables us to create, update, read, and delete the
files stored on the file system through our python program. The following
operations can be performed on a file.
In Python, File Handling consists of following three steps:
? Open the file.
? Process file i.e perform read or write operation.
? Close the file.
File Handling
Types of File
There are two types of files:
Text Files- A file whose contents can be viewed using a text editor is called a text file. A text
file is simply a sequence of ASCII or Unicode characters. Python programs, contents written
in text editors are some of the example of text files.e.g. .txt,.rtf,.csv etc.
Binary Files-A binary file stores the data in the same way as as stored in the memory. The
.exe files,mp3 file, image files, word documents are some of the examples of binary files.we
can’t read a binary file using a text editor.e.g. .bmp,.cdr etc.
Text File Binary File
Its Bits represent character. Its Bits represent a custom data.
Less prone to get corrupt as change reflects as 
soon as made and can be undone.
Can easily get corrupted, corrupt on even single
bit change
Store only plain text in a file.
Can store different types of data (audio, 
text,image) in a single file.
Widely used file format and can be opened in any 
text editor.
Developed for an application and can be opened 
in that application only.
Mostly .txt,.rtf are used as extensions to text files. Can have any application defined extension.
File Handling
Opening and Closing Files-
To perform file operation ,it must be opened first then after reading
,writing, editing operation can be performed. To create any new file
then too it must be opened. On opening of any file ,a file relevant
structure is created in memory as well as memory space is created to
store contents.
Once we are done working with the file, we should close the file.
Closing a file releases valuable system resources. In case we forgot to
close the file, Python automatically close the file when program ends
or file object is no longer referenced in the program. However, if our
program is large and we are reading or writing multiple files that can
take significant amount of resource on the system. If we keep opening
new files carelessly, we could run out of resources. So be a good
programmer , close the file as soon as all task are done with it.
Page 5


Need for 
a data file
• To Store data in organized manner
• To store data permanently
• To access data faster
• To Search data faster
• To easily modify data later on
File Handling
A file is a sequence of bytes on the disk/permanent storage where a group
of related data is stored. File is created for permanent storage of data.
In programming, Sometimes, it is not enough to only display the data on the
console. Those data are to be retrieved later on,then the concept of file
handling comes. It is impossible to recover the programmatically generated
data again and again. However, if we need to do so, we may store it onto
the file system which is not volatile and can be accessed every time. Here,
comes the need of file handling in Python.
File handling in Python enables us to create, update, read, and delete the
files stored on the file system through our python program. The following
operations can be performed on a file.
In Python, File Handling consists of following three steps:
? Open the file.
? Process file i.e perform read or write operation.
? Close the file.
File Handling
Types of File
There are two types of files:
Text Files- A file whose contents can be viewed using a text editor is called a text file. A text
file is simply a sequence of ASCII or Unicode characters. Python programs, contents written
in text editors are some of the example of text files.e.g. .txt,.rtf,.csv etc.
Binary Files-A binary file stores the data in the same way as as stored in the memory. The
.exe files,mp3 file, image files, word documents are some of the examples of binary files.we
can’t read a binary file using a text editor.e.g. .bmp,.cdr etc.
Text File Binary File
Its Bits represent character. Its Bits represent a custom data.
Less prone to get corrupt as change reflects as 
soon as made and can be undone.
Can easily get corrupted, corrupt on even single
bit change
Store only plain text in a file.
Can store different types of data (audio, 
text,image) in a single file.
Widely used file format and can be opened in any 
text editor.
Developed for an application and can be opened 
in that application only.
Mostly .txt,.rtf are used as extensions to text files. Can have any application defined extension.
File Handling
Opening and Closing Files-
To perform file operation ,it must be opened first then after reading
,writing, editing operation can be performed. To create any new file
then too it must be opened. On opening of any file ,a file relevant
structure is created in memory as well as memory space is created to
store contents.
Once we are done working with the file, we should close the file.
Closing a file releases valuable system resources. In case we forgot to
close the file, Python automatically close the file when program ends
or file object is no longer referenced in the program. However, if our
program is large and we are reading or writing multiple files that can
take significant amount of resource on the system. If we keep opening
new files carelessly, we could run out of resources. So be a good
programmer , close the file as soon as all task are done with it.
File Handling
open Function-
Before any reading or writing operation of any file,it must be
opened first
of all.Python provide built in function open() for it.On calling of
this function creates file object for file operations.
Syntax
file object = open(<file_name>, <access_mode>,< buffering>)
file_name = name of the file ,enclosed in double quotes.
access_mode= Determines the what kind of operations can be 
performed with file,like read,write etc.
Buffering = for no buffering set it to 0.for line buffering set it to 
1.if it is greater than 1 ,then it is buffer size.if it is negative then 
buffer size is system default.
Read More
1 videos|25 docs|18 tests

Top Courses for Grade 12

1 videos|25 docs|18 tests
Download as PDF
Explore Courses for Grade 12 exam

Top Courses for Grade 12

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Summary

,

mock tests for examination

,

practice quizzes

,

pdf

,

Detailed Notes: File Handling | Computer Science for Grade 12

,

Important questions

,

Previous Year Questions with Solutions

,

Exam

,

Free

,

study material

,

MCQs

,

ppt

,

video lectures

,

Detailed Notes: File Handling | Computer Science for Grade 12

,

past year papers

,

Detailed Notes: File Handling | Computer Science for Grade 12

,

Objective type Questions

,

shortcuts and tricks

,

Viva Questions

,

Extra Questions

,

Semester Notes

,

Sample Paper

;