You are on page 1of 8

Python Notes Class XII Binary File

Python pickle
Pickle is used for serializing and de-serializing Python object. Serialization refers to the process of
converting an object in memory to a byte stream that can be stored on disk (backing storage). Later on,
this byte stream can then be retrieved and de-serialized back to a Python object (variable). In Python
process of serialization and de-serialization is called is pickling. For pickling in Python has pickle module.

Important functions for binary file handling:


• fobj=open('MYDATA.DAT','wb') #with open('MYDATA.DAT','wb') as fobj:
Built-in function open() creates a file object. Generally, a binary data has extension .DAT, but a
binary data file can have any other extension. When opening a binary data file, the mode parameter
has an extra letter 'b' to indicate that the file MYDATA.DAT is a binary data file.
• pickle.dump(data, fobj) – function from pickle module and it writes values stored in the
variable (object) data, into a binary data file using the file object fobj. Variable data represents
either a list or a dictionary. Function dump() transfers data to the buffer.
• data=pickle.load(fobj) – is function from pickle module and it reads data from a binary
data file using file object fobj, into a variable (object) data through a buffer. Variable data can
either be a list or dictionary type depending how the data was dumped. If the binary file was created
using a list, then pickle.load(fobj) will read a list and store the data in the variable data. If
the binary file was created using a dictionary, then pickle.load(fobj) will read a dictionary
and store the data in the variable data.
• fobj.close() – will close a binary data file using the file object fobj.

Python function to append list type data (every record is a list) in a binary data file type is given below:
import pickle
def addrec():
fobj=open('SALARY.DAT', 'ab')
for k in range(5):
code:int(input('Code? '))
name:input('Name? ')
bsal:float(input('BSal? '))
emp=[code, name.upper(), bsal]
pickle.dump(emp, fobj)
fobj.close()

Variable fobj is a binary data file object created with open(). File mode contains 2-characters string.
Letter 'a' for append mode and letter 'b' for binary. If a binary file is to be opened in write mode,
then letter 'w' will replace letter 'a'. Data to be written into the binary is a list emp. Statement
pickle.dump(emp, fobj) writes a record into a binary file data file. One record is written at a time
in the binary data file. A binary data file can store dictionary type data (every record is dictionary type)
also. Python function to append dictionary type data (every record is a dictionary) in a binary data file
type is given below:
import pickle
def addrec():
fobj=open('SALARY.DAT', 'ab')
for k in range(5):
code:int(input('Code? '))
name:input('Name? ').upper()
bsal:float(input('BSal? '))
emp={'co':code, 'na':name, 'bs':bsal}
pickle.dump(emp, fobj)
fobj.close()

Page 1/8
Python Notes Class XII Binary File
Number of load() must match number dump() when reading from a binary data file. Since it
impossible to remember the count, a strategy has to be devised by which there will be no need to remember
number of load() when reading a from a binary file. Python function to read and display a binary data
file containing list type data (every record is a list) is given below:
import pickle
def showrec():
fobj=open('SALARY.DAT', 'rb')
fobj.seek(0, 2)
eof=fobj.tell()
fobj.seek(0)
while fobj.tell()<eof:
emp=pickle.load(fobj)
print(emp[0],emp[1],emp[2],sep='\t')
fobj.close()

Variable fobj is a binary data file object created with open(). File mode contains 2-characters string.
Letter for 'r' read mode and letter 'b' for binary. Statement emp=pickle.load(fobj) reads a
record from the binary file stores data in a list type variable emp. File object method seek() moves the
file pointer and file object method tell() returns the position of the file pointer.
fobj.seek(0, 2): moves the file pointer to the end of the file
eof=fobj.tell(): returns the position of the file pointer at the end of the file (position of the last
byte of the file)
fobj.seek(0) : moves the file pointer to the beginning of the file
The logic for reading a binary file is read all the records of the file till the end of the file. The while loop
condition, fobj.tell()<eof compares current position of the file pointer. If the current position of
the file pointer is less than position of the last byte, it means at least one more record is yet to be read.
After last record has been read, fobj.tell() and eof will be equal. As menitioned earlier, a binary
data may contain dictionary type data. Python function to read and display a binary data file containing
dictionary type data (every record is a dictionary) is given below:
import pickle
def showrec():
fobj=open('SALARY.DAT', 'rb')
fobj.seek(0, 2)
eof=fobj.tell()
fobj.seek(0)
while fobj.tell()<eof:try:
emp=pickle.load(fobj)
print(emp['co'],emp['na'],emp['bs'],sep='\t')
fobj.close()

Function to search for a code in a binary data file containing list type data (every record is a list), using
a flag variable is given below:
import pickle
def searchcode():
fobj=open('SALARY.DAT', 'rb')
fobj.seek(0, 2)
eof=fobj.tell()
fobj.seek(0)
code=int(input('Code to Search? '))
found=0
while fobj.tell()<eof:
emp=pickle.load(fobj)
if code==emp[0]:
Page 2/8
Python Notes Class XII Binary File
print(emp[0],emp[1],emp[2],sep='\t')
found=1
break
fobj.close()
if found==0:
print(code, 'Not Found in the File')
OR,
Function to search for a code in a binary data file containing list type data (every record is a list), without
using a flag variable is given below:
import pickle
def searchcode():
fobj=open('SALARY.DAT', 'rb')
fobj.seek(0, 2)
eof=fobj.tell()
fobj.seek(0)
code=int(input('Code to Search? '))
while fobj.tell()<eof:
emp=pickle.load(fobj)
if code==emp[0]:
print(emp[0],emp[1],emp[2],sep='\t')
found=1
break
else:
print(code, 'Not Found in the File')
fobj.close()

Function to search for name in a binary data file (containing list type of data), using a flag variable is
given below (assuming all names are distinct):
import pickle
def searchname():
fobj=open('SALARY.DAT', 'rb')
fobj.seek(0, 2)
eof=fobj.tell()
fobj.seek(0)
name=input('Name to Search? ').upper()
found=0
while fobj.tell()<eof:
emp=pickle.load(fobj)
if name==emp[1]:
print(emp[0],emp[1],emp[2],sep='\t')
found=1
break
fobj.close()
if found==0:
print(name, 'Not Found in the File')
OR,
Function to search for name in a binary data file (containing list type of data), without using a flag variable
is given below (assuming all names are distinct):
import pickle
def searchname():
fobj=open('SALARY.DAT', 'rb')
fobj.seek(0, 2)
eof=fobj.tell()
fobj.seek(0)
Page 3/8
Python Notes Class XII Binary File
name=input('Name to Search? ').upper()
while fobj.tell()<eof:
emp=pickle.load(fobj)
if name==emp[1]:
print(emp[0],emp[1],emp[2],sep='\t')
found=1
break
else:
print(name, 'Not Found in the File')
fobj.close()

Function to search for salary in a binary data file (containing list type of data) is given below:
import pickle
def searchbsal():
fobj=open('SALARY.DAT', 'rb')
fobj.seek(0, 2)
eof=fobj.tell()
fobj.seek(0)
while fobj.tell()<eof:
emp=pickle.load(fobj)
if emp[2]>150000.0:
print(emp[0],emp[1],emp[2],sep='\t')
c+=1
fobj.close()
print('Number of Records=',c)
OR,
def searchbsal():
fobj=open('SALARY.DAT', 'rb')
fobj.seek(0, 2)
eof=fobj.tell()
fobj.seek(0)
c=0
while fobj.tell()<eof:
emp=pickle.load(fobj)
if emp[2]>150000.0:
print(emp[0],emp[1],emp[2],sep='\t')
c+=1
fobj.close()
if c==0:
print('No Records Found in the File')

Function to edit all the records in a binary data file (containing list type of data) using a temporary file
is given below:
import pickle
from os import remove, rename
def editallrecs():
frobj=open('SALARY.DAT', 'rb')
fwobj=open('TEMP.DAT', 'wb')
frobj.seek(0, 2)
eof=frobj.tell()
frobj.seek(0)
while frobj.tell()<eof:
emp=pickle.load(frobj)
emp[2]+=10000
Page 4/8
Python Notes Class XII Binary File
pickle.dump(emp, fwobj)
frobj.close()
fwobj.close()
remove('SALARY.DAT')
rename('TEMP.DAT','SALARY.DAT')

Function to edit all the records in a binary data file (containing list type of data) using a list is given
below:
import pickle
def editallrecs():
fobj=open('SALARY.DAT', 'rb')
fobj.seek(0, 2)
eof=fobj.tell()
fobj.seek(0)
emplist=[]
while fobj.tell()<eof:
emp=pickle.load(fobj)
emp[2]+=10000
emplist+=[emp]
fobj.close()
fobj=open('SALARY.DAT', 'wb')
for emp in emplist:
pickle.dump(emp, fobj)
fobj.close()

Function to edit a particular record (by inputting employee code) in a binary data file (containing list
type of data) using a temporary file is given below:
import pickle
from os import remove, rename
def editonerec():
frobj=open('SALARY.DAT', 'rb')
fwobj=open('TEMP.DAT', 'wb')
frobj.seek(0, 2)
eof=frobj.tell()
frobj.seek(0)
code=int(input('Input Code to Edit? '))
found=0
while frobj.tell()<eof:
emp=pickle.load(frobj)
if code==emp[0]:
emp[2]+=10000
found=1
pickle.dump(emp, fwobj)
frobj.close()
fwobj.close()
remove('SALARY.DAT')
rename('TEMP.DAT','SALARY.DAT')
if found==1:
print('Record Updated in the File')
else:
print(code, 'Not Found in the File!!')

Function to edit a particular record (by inputting employee code) in a binary data file (containing list
type of data) using a list is given below:
Page 5/8
Python Notes Class XII Binary File
import pickle
def editonerec():
fobj=open('SALARY.DAT', 'rb')
fobj.seek(0, 2)
eof=fobj.tell()
fobj.seek(0)
code=int(input('Input Code to Edit? '))
found=0
emplist=[]
while fobj.tell()<eof:
emp=pickle.load(robj)
if code==emp[0]:
emp[2]+=10000
found=1
emplist+=[emp]
fobj.close()
fobj=open('SALARY.DAT', 'wb')
for emp in emplist:
pickle.dump(emp, fobj)
fobj.close()
if found==1:
print('Record Updated in the File')
else:
print(code, 'Not Found in the File!!')

Function to delete a record from a binary data file (containing list type of data) using a temporary file is
given below:
import pickle
from os import remove, rename
def deleterecord():
frobj=open('SALARY.DAT', 'rb')
fwobj=open('TEMP.DAT', 'wb')
frobj.seek(0, 2)
eof=frobj.tell()
frobj.seek(0)
code=int(input('Code to Delete? '))
found=0
while frobj.tell()<eof:
emp=pickle.load(frobj)
if code==emp[0]:
found=1
else:
pickle.dump(emp, fwobj)
frobj.close()
fwobj.close()
remove('SALARY.DAT')
rename('TEMP.DAT','SALARY.DAT')
if found==1:
print('Record Deleted From the File')
else:
print(code, 'Not Found in the File')

Function to delete a record from a binary data file (containing list type of data) using a list is given below:
import pickle
Page 6/8
Python Notes Class XII Binary File
def deleterecord():
fobj=open('SALARY.DAT', 'rb')
fobj.seek(0, 2)
eof=fobj.tell()
fobj.seek(0)
code=int(input('Code to Delete? '))
found=0
emplist=[]
while fobj.tell()<eof:
emp=pickle.load(fobj)
if code==emp[0]:
found=1
else:
emplist+=[emp]
fobj.close()
fobj=open('SALARY.DAT', 'wb')
for emp in emplist:
pickle.dump(emp, fobj)
fobj.close()
if found==1:
print('Record Deleted From the File')
else:
print(code, 'Not Found in the File')

File Pointer and Binary File


Now with binary file we will see the use of seek(pos, whence). Binary data file 'EMPSAL.DAT'
contains records as [CODE, NAME, BSAL, HRA, GSAL].
Content of EMP.DAT with the position of file pointer after every record:
2001 GUATAM SHARMA 155000.0 31000.0 186000.0 62
2002 DEEPIKA RAO 166000.0 33200.0 199200.0 122
2003 JATHIN KUMAR 155000.0 31000.0 186000.0 183
2004 ADITI GUPTA 150000.0 30000.0 180000.0 243
2005 ASEEM KAPUR 170000.0 34000.0 204000.0 303
2006 TINA DASH 155000.0 31000.0 186000.0 361
2007 KUNAL THAKUR 155000.0 31000.0 186000.0 422
2008 DILIP SHAW 155000.0 31000.0 186000.0 481
2009 JYOTHI SURESH 185000.0 37000.0 222000.0 543
2010 MITALI JHA 160000.0 32000.0 192000.0 602
import pickle
fobj=open('EMP.DAT','rb')
fobj.seek(122)
emp=pickle.load(fobj)
print(emp[0],emp[1],emp[2],sep='\t')
fobj.seek(120,1)
emp=pickle.load(fobj)
print(emp[0],emp[1],emp[2],sep='\t')
fobj.seek(182,1)
emp=pickle.load(fobj)
print(emp[0],emp[1],emp[2],sep='\t')
fobj.seek(-180,2)
emp=pickle.load(fobj)
print(emp[0],emp[1],emp[2],sep='\t')

Page 7/8
Python Notes Class XII Binary File
Running of the program produces following output:
2003 JATHIN KUMAR 155000.0 31000.0 186000.0
2006 TINA DASH 155000.0 31000.0 186000.0
2010 MITALI JHA 160000.0 32000.0 192000.0
2008 DILIP SHAW 155000.0 31000.0 186000.0

Explanation of the output:


fobj.seek(122) moves the file pointer from the beginning of the file, to the end of
second record and the beginning of the third record
emp=pickle.load(fobj) reads the third record from the file
print(emp) displays third record
fobj.seek(120,1) moves the file pointer 120 bytes forward from the current location
(183 bytes), to the end of fifth record and the beginning of the sixth
record
emp=pickle.load(fobj) reads the sixth record from the file
print(emp) displays sixth record
fobj.seek(182,1) moves the file pointer 182 bytes forward from the current location
(361 bytes), to the end of ninth record and the beginning of the tenth
record
emp=pickle.load(fobj) reads the tenth record from the file
print(emp) displays tenth record
fobj.seek(-180,2) moves the file pointer 180 bytes backward from the end of the file to
the end of seventh record and the beginning of the eight record
emp=pickle.load(fobj) reads the eight record from the file
print(emp) displays eight record

Page 8/8

You might also like