Q. Write a program to increase the salary by Rs. 2000/- of the employee having empno as 1251 in the file empl.dat.
You can understand by Watching video :-
Answer :-
import pickle emp = {} found = False fin = open('empl.dat', 'rb+') fin.seek (0) try: while True : rpos = fin.tell() emp = pickle.load(fin) if emp['Empno'] == 1251 : emp[ 'Salary'] = emp['Salary'] + 2000 fin.seek (rpos) pickle.dump(emp, fin) print (emp) found = True except EOFError: if found == False: print("Sorry, no matching record found.")] else: print("Record(s) successfully updated.") fin.close()
For Output, At frist we have to write something in the emp.dat file by the following code :-
import pickle f = open("empl.dat","wb") while True : empno = int(input ("Enter Empno. ( for exit enter -1 ):- ")) if empno == -1 : print() break name = input( "Enter Name of Employee :-") sal = float(input("Enter Salary of Employee :- ")) dic = { 'Empno' : empno , "Name" : name, 'Salary' : sal } pickle.dump( dic , f ) print() print("All Data are written Successfully ") f.close()
Output writing script :-
Enter Empno. ( for exit enter -1 ):- 7854
Enter Name of Employee :-Path
Enter Salary of Employee :- 4758
Enter Empno. ( for exit enter -1 ):- 4557
Enter Name of Employee :-WaLLA
Enter Salary of Employee :- 46546.74
Enter Empno. ( for exit enter -1 ):- 1251
Enter Name of Employee :-Portal
Enter Salary of Employee :- 599
Enter Empno. ( for exit enter -1 ):- -1
All Data are written Successfully
>>>
Enter Name of Employee :-Path
Enter Salary of Employee :- 4758
Enter Empno. ( for exit enter -1 ):- 4557
Enter Name of Employee :-WaLLA
Enter Salary of Employee :- 46546.74
Enter Empno. ( for exit enter -1 ):- 1251
Enter Name of Employee :-Portal
Enter Salary of Employee :- 599
Enter Empno. ( for exit enter -1 ):- -1
All Data are written Successfully
>>>
The output of the Main Program :-
{'Empno': 1251, 'Name': 'Portal', 'Salary': 2599.0}
Record(s) successfully updated.
>>>
Record(s) successfully updated.
>>>
Please send the output 🙏🏻
ReplyDeleteOk,
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )