chapter-4-data-file-handlingeng
chapter-4-data-file-handlingeng
Based on CBSE
Curriculum
Class-
12
Data File Handling
• We have seen yet only the transient programs. The programs
which run for a short period of time and give some output
and after that their data is disappeared. And when we again run
those programs then we have to use new data.
• This is because the data is entered in primary memory which is
temporary memory and its data is volatile.
• Those programs which are persistent i.e. they are always in
running or run for a long time then their data is
stored in permanent storage (e.g. harddisk) . If the
program is closed or restarted then the data used will be
retrieved.
• For this purpose the program should have the capability to
read or write the text files or data files. These files can be saved
in permanent storage.
• The meaning of File I/O (input-output) is to transfer the data
from Primary memory to secondary memory and vice-versa.
(Random Access Hard
User
Memory)
Program in RAM Disk
Why the Files are used?
• The data stored with in a file is known as persistent data
because
this data is permanently stored in the system.
• Python provides reading and writing capability of data files.
• We save the data in the files for further use.
• As you save your data in files using word, excel etc. same
thing we
can do with python.
• “A File is a collection of characters in which we can
perform read and write functions. And also we can save it
Write to file
in secondary storage.” (Save)
Python External
Program File
(Secondar
y Storage)
Read from
file (Load)
Data File Operations
Following main operations can be done on
files -
1. Opening a file
2. 1.Performing
READ operations
2. WRITE etc.
3. Closing The
File
Open Process Close
File Data File
Beside above operations there are some more operations can be
done on
files.-
• Creating of Files
• Traversing of Data
• Appending Data into file.
• Inserting Data into File.
• Deleting Data from File.
• Copying of File.
• Updating Data into File.
Neha Tyagi, KV No-5, Jaipur
File Types
File are of two types –
1. Text File: A text file is sequence of line and line is
the sequence of characters and this file is saved in
a permanent storage device. Although in python
default character coding is ASCII but by using
constant ‘U’ this can be converted into UNICODE.
In Text File each line terminates with a special
character which is EOL (End Of Line). These are in
human readable form and these can be
created using any text editor.
2. Binary File: Binary files are used to store binary data
such as images, videos audio etc. Generally
numbers are stored in binary files. In binary file,
there is no delimiter to end a line. Since they are
directly in the form of binary hence there is no need
to translate them. That’s why these files are easy
and fast in working. Neha Tyagi, KV No-5, Jaipur
Opening & Closing Files
• We need a file variable or file handle to work with files in
Python.
• This file object can be created by using open( ) function
or file( ) function.
• Open( ) function creates a file object, which is used later to
access
the file using the functions related to file manipulation.
• Itssyntax is following -
<file_object>=open(<file_name>,<access_mode>)
• File accessing modes -
– write(w): to write to the file Python External
– read(r): To read the file
Program File
– append(a): to Write at the end of
(Seconda
file.
ry
Storage)
Read from
file (Load)
Neha Tyagi, KV No-5, Jaipur
Opening & Closing Files. . .
Opened the File
Here the point is that the file “Hello.txt” which is used here is pre
built and stored in the same folder where Python is installed.
A Program to read
“Hello.txt” File.
Output
Output
Output
Writing to a File. . .
Output
“Hello.txt” File is
created using the
above program.
Writing to a File.
Hello.txt file is opened using “with”.
Output
“Hello.txt” File is
created using the
above program.
Appending in a File
• Append means adding something new to existing file.
• ‘a’ mode is used to accomplish this task. It means
opening a file in write mode and if file is existing then
adding data to the end of the file.
A program to append
into a file “Hello.Txt”
Output
Taking data
the user from
and writing
data this the
to file
“Stud nt.txt”.
Student File is
e created by using
the above
Output program.
Operations in Binary File.
• If we want to write structure such as list, dictionary etc and
also we want to read it then we have to use a module in
python known as pickle.
• Pickling means converting structure into byte stream
before writing the data into file.
• And when we read a file then a oppositeoperation is to
bedone means unpickling.
• Pickle module has two methods - dump( ) to write and load( )
to read.
Operations in Binary File
• To read Binary file use of load ( )
function -
Relative and Absolute Paths
• We all know that the files are kept in directory which
are also known as folders.
• Every running program has a current directory.
Which is generally a default directory and python
always see the default directory first.
• OS module provides many such functions which can be
used to work with files and directories. OS means
Operating System.
• getcwd( ) is a very function which can be used to
identify the
current working directory.
Standard File Streams
• We use standard I/O Streams to
get better performance from different I/O
devices.
• Some Standard
– Standard input Streams in sys.stdin
python are as follows -
Stream sys.stdo
– Standard output ut
Stream sys.stder
– Standard error
r
Stream