CLASS-XII
COMPUTER SCIENCE
DATA FILE HANDING
(BINARY DATA FILE)
MANASH RANJAN SAHOO
PGT(COMP. SC.)
KENDRIAYA VIDYALAYA NO.4, BHUBANESWAR
DATA FILES
It is a separate file than the code file(.py) in python
which contains data pertaining to specific
application, for later use. This may be of following
type:
TEXT FILES
BINARY FILES
CSV(COMMA SEPARATED VALUE ) FILES
BINARY FILES
1. A Binary file contain arbitrary binary
data
2. Binary files are used to store binary data
such as image, video, audio, text
3. There is no delimiter
4. Absence of delimiter makes files to
process fast while reading or writing
operations are performed on file and hence
preferred from text file..
FILE ACCESS MODES
‘wb’ – Open a file for write only mode in the binary
format.
‘rb’ – Open a file for the read-only mode in the binary
format.
‘ab’ – Open a file for appending only mode in the binary
format.
‘rb+’ – Open a file for read and write only mode in the
binary format.
‘ab+’ – Open a file for appending and read-only mode in
the binary format.
FILE MODES AND OPENING
POSITION OF THE FILE POINTER
STEPS IN DATA FILE HANDLING
OPENING OF DATA FILE
We should first open the data file for read and write
by specifying the name of file and mode.
PERFORMING READ AND WRITE OPERATION
Once the data file is opened now we can either read
or write for which file is opened using various
functions available.
CLOSING FILE
After performing operations we must close the file
and release the file for other applications to use it.
ABSOLUTE AND RELATIVE PATH
ABSOLUTE PATH
Absolute path of file is file location
where it starts from the top most directory in
the file system.
ABSOLUTE
PATH
RELATIVE PATH
Relative Path of file is file location,
where it starts from the current working
directory
\\Datafile.txt
RELATIVE
PATH
BINARY FILE OPERATIONS
If we want to write a structure such as a List
or Dictionary to a data file and read it
subsequently , we need to use the Python
module pickle.
Serialisation (Pickling) is the process of
converting python object hierarchy to a byte-
stream before writing to a file and while
reading the content of a data file a reverse
process called Unpickling is used to convert
the byte stream back to the python object
PICKLING AND UNPICKLING
USING PICKLE MODULE
First we need to import the pickle
module by command
import pickle
It provides two main methods for the
purpose:-
1) dump() method
2) load() method
PICKLE.DUMP() METHOD
Use pickle.dump() method to write the
object in file which is opened in binary
access mode.
Syntax of dump method is:
dump(object,fileobject)
PICKLE.DUMP() METHOD
# A program to write list sequence in a binary file
OUTPUT
PICKLE.LOAD() METHOD
pickle.load() method is used to read the
binary file.
pickle.load() method is used to read
the binary file.
Q1.CREATE A BINARY FILE NAMELY
MYFILE.TXT AND WRITE A STRING
HAVING THREE LINES IN IT
Answer:-
import pickle
string=“ My first line, My second line, My third line.”
with open (“myfile.txt”, “wb”) as ft:
pickle.dump(string, ft)
print(“File successfully created.”)
UPDATING IN A BINARY FILE
We know that updating an object means changing its value(s) and
string it again. Updating records in a file is similar and is a three-
step process, which is
Locate the record to be updated by searching for it.
Make changes in the loaded records in memory
Write back onto the file at the exact location of old records
The first two steps we are known about but the third steps is important
as we have to obtain the location of the record in the file where to
place the file pointer for writing the record after updating.
ACCESSING AND MANIPULATING
LOCATION OF FILE POINTER-RANDOM
ACCESS
The two file-pointer location functions of Python are :
tell( )
seek ( )
tell ( ): This function returns the current position of file pointer in the file. It is used
as per the following syntax:
file_object.tell()
seek ( ): This function changes the position of the file-pointer by placing the file-
pointer at the specified position in the open file. The syntax is:
file_object.seek( offset[, mode])
(offset= -ve means in backward direction and offset=+ve means forward direction.
If mode=1 means ahead of current file pointer position, mode=2 means from end
of file, mode=0 means from the beginning of the file)
EXAMPLES OF SEEK() AND TELL()
Q. Check the position of file pointer Q. Read the last 14 bytes of the file
after read() function “students.txt”)
Answer: Answer:
f=open(“students.txt”, “r”) f=open(“students.txt”, “r”)
print(f.read()) f.seek(-14,2)
print(“File pointer is now at byte:”, str1=f.read(14)
f.tell( ))
print(“Last 14 bytes of file contains:”,
OUTPUT str1)
Asha
OUTPUT
Jyotiraditya
Last 14 bytes of file contains :Dipti
Dipti
THANK YOU