A binary file “emp.dat” has structure [EID, Ename, designation, salary].

A binary file “emp.dat” has structure [EID, Ename, designation,
salary].
i. Write a user defined function CreateEmp() to input data for
a record and create a file emp.dat.
ii. Write a function display() in Python to display the detail of
all employees whose salary is more than 50000.

1 Answer

  1. (i)

    import pickle
    def CreateEmp():
    f1=open("emp.dat",'wb')
    eid=input("Enter E. Id")
    ename=input("Enter Name")
    designation=input("Enter Designation")
    salary=int(input("Enter Salary"))
    l=[eid,ename,designation,salary]
    pickle.dump(l,f1)
    f1.close()

    (ii)

    import pickle
    def display():
    f2=open("emp.dat","rb")
    try:
    while True:
    rec=pickle.load(f2)
    if rec[3]>5000:
    print(rec[0],rec[1],rec[2],rec[3])
    except:
    f2.close()

You must login to add an answer.

Related Questions