Monday, September 5, 2022

Binary File Handling - Operation using Using inbuilt Data Structure(List, Dictionary etc)

Binary File Handling - Operation using Using inbuilt Data Structure(List, Dictionary etc)

Content:

  • Introduction
  • Writing/Reading dictionary into a binary file:
  • Searching data in binary file
  • Updating data in binary file

Introduction:

In python, inbuilt data can be stored in binary file and operated upon. The reason behind this is that all inbult data structure are objects in python and we have seen how objects can be stored and operated upon(LINK). We can use the same load() and dump() methods in the pickle module to read and write data in a binary file. Also we can use the tell() and seek() method to navigate the file pointer in a binary file.

Writing/Reading dictionary into a binary file:
Sample Programs:

  1. Write program to store employee details(name,age,post,salary) in a binary file(empData.dat) using dictionary datastructure. The employee data should be taken as input from the user.
In [17]:
import pickle
empNo=int(input("Enter the number of employee: "))
dc={}
fl=open("empData.dat","wb")
for i in range(empNo):
    nm=input("Name of the Employee: ")
    ag=input("Age of the Employee: ")
    post=input("Post of the Employee: ")
    salary=input("Salary of the Employee: ")
    dc["name"]=nm
    dc["age"]=ag
    dc["post"]=post
    dc["salary"]=salary
    pickle.dump(dc,fl)
    dc={}
    
fl.close()
    
Enter the number of employee: 2
Name of the Employee: Ted
Age of the Employee: 32
Post of the Employee: manager
Salary of the Employee: 52000
Name of the Employee: John
Age of the Employee: 36
Post of the Employee: CEO
Salary of the Employee: 120000
  1. Write a program to display all the data stored in the empData.dat
In [15]:
import pickle

fl=open("empData.dat","rb")
while True:
    try:
        dt=pickle.load(fl)
        print(dt)
    except EOFError:
        break
        fl.close()      
{'name': 'Ted', 'age': '32', 'post': 'manager', 'salary': '52000'}
{'name': 'John', 'age': '36', 'post': 'CEO', 'salary': '120000'}

Searching data in binary file:
Sample program:

  1. Write a program to find an employee by name from a binary file(empdata.dat), where teh data is stored as dictionary object. Note: the employee name to be searched must be taken as input from the user.
In [6]:
import pickle

fl=open("empData.dat","rb")
cn=0
nm=input("Name of the Employee to search: ")
while True:
    
    try:
        dt=pickle.load(fl)        
        if dt['name']==nm:
            print(dt)
            cn+=1
            break
           
    except EOFError:        
        
        fl.close()  
        break
if cn==0:
    print("Employee does not exists")
Name of the Employee to search: John
{'name': 'John', 'age': '36', 'post': 'CEO', 'salary': '120000'}

Updating data in binary file:
Sample Program:

  1. Write a program to update any particular data of an employee from the empdata.dat binary file. Note: the name of the employee and the data to be updated should be taken from the user.
In [19]:
import pickle
nm=input("Enter the Name of the employee whose data needs to be changed")
key=input("Which data you need to change")
val=input("Updated data to be stored")
fl=open("empdata.dat","r+b")
pos=0
while True:
    
    try:
        
        dt=pickle.load(fl)
        pos=fl.tell()
        
        if dt['name']==nm:
            dt[key]=val
            fl.seek(pos)
            print(dt)
            pickle.dump(dt,fl)
            fl.close()
            break
    except EOFError:
        fl.close()
        break

fl=open("empData.dat","rb")
while True:
    try:
        dt=pickle.load(fl)
        print(dt)
    except EOFError:
        break
        fl.close()      
Enter the Name of the employee whose data needs to be changedTed
Which data you need to changeage
Updated data to be stored52
{'name': 'Ted', 'age': '52', 'post': 'manager', 'salary': '52000'}
{'name': 'Ted', 'age': '32', 'post': 'manager', 'salary': '52000'}
{'name': 'Ted', 'age': '52', 'post': 'manager', 'salary': '52000'}
In [ ]:
 
Share: