Monday 8 February 2021

Practical No. 9 : Create a binary file with eid, ename and salary and update the salary of the employee.

 





'''Practical No: 9: WAP in Python to create a binary file with eid, ename and 

salary of the employees and update the salary of specific employee.'''

import pickle

E={}

f=open('emp.dat','wb')

c='y'

while c=='y' or c=='Y':

    eid=int(input("Enter the Emp Id of the Employee : "))

    ename=input("Enter the name of the Employee: ")

    salary=float(input("Enter the salary of the Employee: "))

    E['Emp_Id']=eid

    E['Emp_Name']=ename

    E['Salary']=salary

    pickle.dump(E,f)

    c=input("Do You Want to add more employee(y/n): ")

f.close()

f=open('emp.dat','rb+')

eid=int(input("Enter the Emp Id of the employee to be updated: "))

salary=float(input("Enter the updated salary of the employee: "))

f.seek(0,0)

m=0

try:

    while True:

        pos=f.tell()

        E=pickle.load(f)

        if E["Emp_Id"] == eid:

            f.seek(pos)

            E["Salary"]=salary

            pickle.dump(E,f)

            m=m+1

except EOFError:

    f.close()

if m==0:

        print("Employee not Found")

else:      

    f=open('emp.dat','rb')

    try:

        while True:

            E=pickle.load(f)

            print(E)

    except EOFError:

        f.close()




No comments:

Post a Comment