NextStep is an organization which has a pool of resource persons to conduct training workshops on various topics related to ICT. The data of all its Resource Persons is stored in a binary file RESOURCES.DAT using the following record structure (each record is a tuple):
(R_ID, R_Name, R_Expertise, Charges)
where R_ID – Resource Person's ID (An integer); R_Name – Resource Person's Name (A string); R_Expertise – Area of expertise of the Resource Person; Charges – Charges (in rupees) per hour to conduct a workshop.
For example, a record in the file is:
(12, 'P. Velusami','Machine Learning',5000)In this context, write the following user defined functions in Python:
Append() – To input the data of a Resource Person and write it in the file RESOURCES.DAT.
import pickle
def Append():
with open('RESOURCES.DAT', 'ab') as fout:
rid = int(input('Resource ID: '))
name = input('Name: ')
exp = input('Expertise: ')
chg = int(input('Charges per hour: '))
rec = (rid, name, exp, chg)
pickle.dump(rec, fout)
print('Record added') Update() – To increase the Charges of each resource person by 500.
import pickle
def Update():
records = []
with open('RESOURCES.DAT', 'rb') as fin:
try:
while True:
records.append(pickle.load(fin))
except EOFError:
pass
records = [(r[0], r[1], r[2], r[3] + 500) for r in records]
with open('RESOURCES.DAT', 'wb') as fout:
for r in records:
pickle.dump(r, fout) | List-I | List-II |
| (A) f.seek(-10,1) | (I) From beginning of file, move 10 bytes forward |
| (B) f.seek(10,1) | (II) From current position, move 10 bytes backward |
| (C) f.seek(10) | (III) From current position, move 10 bytes forward |
| (D) f.seek(-10,2) | (IV) From end of the file, move 10 bytes backward |
Arrange the following in correct order of exception handling in python:
(A) Write the code that may raise an exception inside a try block
(B) Execute some code regardless of whether the exception occurs or not using the finally block
(C) Handle the specific exception using the except block
(D) Raise the exception using the raise statement if necessary
Choose the correct answer from the options given below: