Comprehension

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:

Question: 1

Append() – To input the data of a Resource Person and write it in the file RESOURCES.DAT.

Show Hint

Open RESOURCES.DAT in 'ab' mode, take the four values as input, make a tuple and write it with pickle.dump().
Updated On: Oct 20, 2026
Show Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Pick the right file mode:
The file RESOURCES.DAT already holds earlier records.
A new record must be added at the end, so the file is opened in the append binary mode 'ab'.
The mode 'wb' would erase the old records, so it is wrong here.

Step 2: Read the four fields:
The record is a tuple (R_ID, R_Name, R_Expertise, Charges).
The ID and the charges are numbers, so they are converted with int().
The name and the expertise stay as strings.

Step 3: Write the record with pickle:
The module pickle is imported first.
The function dump() writes the tuple to the binary file.
The file is closed at the end.

Step 4: Function code:
import pickle

def Append():
    f = open('RESOURCES.DAT', 'ab')
    r_id = int(input('Enter resource person ID: '))
    r_name = input('Enter name: ')
    r_exp = input('Enter area of expertise: ')
    charges = int(input('Enter charges per hour: '))
    rec = (r_id, r_name, r_exp, charges)
    pickle.dump(rec, f)
    f.close()


Step 5: Test result:
I ran this function twice, with (12, 'P. Velusami', 'Machine Learning', 5000) and (13, 'A. Rao', 'Python', 3000).
Reading the file back gave both tuples, in the order they were added.

Final Answer:
Append() opens RESOURCES.DAT in 'ab' mode, builds the tuple from user input, and writes it with pickle.dump().
Was this answer helpful?
0
0
Question: 2

Update() – To increase the Charges of each resource person by 500.

Show Hint

Load all tuples with pickle.load() until EOFError, build new tuples with Charges + 500, then rewrite the file with 'wb' and pickle.dump().
Updated On: Oct 20, 2026
Show Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Understand the problem with binary files:
A pickled record cannot be edited in place in a safe way. The new record may need more bytes than the old one.
So the safe plan is to read all records, change them, and write them all back.

Step 2: Read all records into a list:
Open the file in 'rb' mode.
Call pickle.load() again and again inside a try block.
When the file ends, pickle.load() raises EOFError, and we stop the loop there.
Every tuple is added to a list.

Step 3: Change the charges:
A tuple cannot be changed, so we build a new tuple for each record.
The first three fields are copied, and the charges field becomes rec[3] + 500.

Step 4: Write everything back:
Open the file in 'wb' mode. This clears the old content.
Dump every new tuple one by one.

Step 5: Function code:
import pickle

def Update():
    f = open('RESOURCES.DAT', 'rb')
    data = []
    try:
        while True:
            data.append(pickle.load(f))
    except EOFError:
        pass
    f.close()
    f = open('RESOURCES.DAT', 'wb')
    for rec in data:
        new_rec = (rec[0], rec[1], rec[2], rec[3] + 500)
        pickle.dump(new_rec, f)
    f.close()


Step 6: Test result:
The file held (12, 'P. Velusami', 'Machine Learning', 5000) and (13, 'A. Rao', 'Python', 3000).
After Update() it held (12, 'P. Velusami', 'Machine Learning', 5500) and (13, 'A. Rao', 'Python', 3500).

Final Answer:
Update() loads all tuples, adds 500 to Charges in each, and rewrites the file with 'wb'.
Was this answer helpful?
0
0