Data File Handling
Data File Handling
Class XII
Data File Handling
K K PRATIHASTA
PGT- CS
JAWAHAR NAVODAYA VIDYALAYA BIRAULI DISTT- SAMASTIPUR
Objective
In this video, you'll learn about Python file operations. More specifically, opening a file,
reading from it, writing into it, closing it, and various file methods that you should be
aware of.
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.
File Types
File are of two types –
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.
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.
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.
Opening & Closing Files
Before any reading or writing operation of any file, it must be opened first
of all. This file object can be created by using open( ) function .
Open( ) function creates a file object, which is used later to access the file using the
functions related to file manipulation.
f=open( "poem.txt“, r )
data=f .readline()
print(data)
data=f .readline()
print(data)
f .close()
Output
When things go wrong as they sometimes will;
We will now discuss the four major operations performed using a binary
file such as—
1. Inserting/Appending record in a binary file.
2. Read records from a binary file.
3. Search a record in a binary file.
4. Update a record in a binary file
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 opposite operation is to be done
means unpickling.
• Pickle module has two methods –
• dump( ) to write
• load( ) to read.
Insert/Append a Record in Binary File
Inserting or adding (appending) a record into a binary file requires importing pickle
module into your program followed by dump() method to write onto the file.
import pickle
mylist = [ 'a', 'b', 'c', 'd‘ ]
f=open( “datafile.txt” , ‘wb‘ )
pickle.dump(mylist, f)
f .close()
Reading a record from a binary file
The following practical implementation illustrates how a record is read from a binary file.
To read Binary file use of load ( ) function -
Searching a record in a binary file
Searching the binary file “datafile" is carried out on the basis of the value entered by
the user. The file is opened in the read-binary mode.
Updating a record in a binary file
Updating a record in the file requires value to be fetched from the user whose value is
to be updated.
RANDOM ACCESS IN FILES USING TELL() AND SEEK()
seek()—seek() function is used to change the position of the file pointer to a given specific
position. The reference point is defined by the "from_what" argument. It can have any of
the three values:
0: sets the reference point at the beginning of the file, which is by default.
1: sets the reference point at the current file position.
2: sets the reference point at the end of the file.