English

Aman is a Python programmer. He has written a code and created a binary file record.dat with employeeid, ename, and salary. The file contains 10 records. - Computer Science (Python)

Advertisements
Advertisements

Question

Aman is a Python programmer. He has written a code and created a binary file record.dat with employeeid, ename, and salary. The file contains 10 records.

He now has to update a record based on the employee id entered by the user and update the salary. The updated record is then to be written in the file temp.dat. The records which are not to be updated also have to be written to the file temp.dat. If the employee id is not found, an appropriate message should be displayed.

As a Python expert, help him to complete the following code based on the requirement given above:

import _______ #Statement 1
def update_data():
rec={}
fin=open("record.dat","rb") 
fout=open("_____________") #Statement 2
found=False
eid=int(input("Enter employee id to update their salary :: "))
while True:
try:
rec=______________ #Statement 3
if rec["Employee id"]==eid:
 found=True 
   rec["Salary"]=int(input("Enter new salary :: "))
   pickle.____________ #Statement 4
else:
   pickle.dump(rec,fout)
except:
  break
if found==True:
print("The salary of employee id ",eid," has
been updated.")
else:
print("No employee with such id is not found")
 fin.close()
fout.close()
  1. Which module should be imported into the program? (Statement 1)
  2. Write the correct statement required to open a temporary file named temp.dat. (Statement 2)
  3. Which statement should Aman fill in Statement 3 to read the data from the binary file, record.dat, and in Statement 4 to write the updated data in the file, temp.dat?
Short Note

Solution

  1. pickle
  2. fout=open(‘temp.dat’, ‘wb’)
  3. Statement 3: pickle.load(fin)
    Statement 4: pickle.dump(rec,fout)
shaalaa.com
The Pickle Module
  Is there an error in this question or solution?
2022-2023 (March) Sample
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×