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

Solution and Explanation

Step 1: Know what a binary file of tuples looks like:
Each call of pickle.dump() stores one tuple in the file, one after the other.
So adding a person means one more dump() at the end of the file.

Step 2: Open the file safely:
We use the with statement. It closes the file by itself, even if an error occurs.
The mode is 'ab'. It keeps the old data and writes new data at the end.
If the file does not exist, 'ab' creates it.

Step 3: Build the record from input:
The four values are typed by the user.
R_ID and Charges are changed to int, because the format shows them as numbers.
Then the four values are put in a tuple, in the same order as the file structure.

Step 4: Write the function:
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')


Step 5: Run it:
Two calls with sample data printed 'Record added' twice.
A read loop with pickle.load() then showed both tuples.

Final Answer:
The function adds one tuple at the end of RESOURCES.DAT using 'ab' mode and pickle.dump().
Was this answer helpful?
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

Solution and Explanation

Step 1: Plan the three phases:
Phase one is load, phase two is modify, phase three is save.
This plan works for any change to every record of a binary file.

Step 2: Phase one, load:
We open RESOURCES.DAT with the with statement in 'rb' mode.
The loop while True keeps calling pickle.load(). It ends when EOFError is raised at the end of the file.
The tuples are kept in the list records.

Step 3: Phase two, modify:
Tuples are immutable, so rec[3] = rec[3] + 500 is not allowed on a tuple.
A list comprehension makes a new tuple for each old one.
The new tuple keeps R_ID, R_Name and R_Expertise and carries the charges plus 500.

Step 4: Phase three, save:
The file is opened again in 'wb' mode. This removes the old content.
Each new tuple is written with pickle.dump().
We do not use seek() to overwrite in place, because a bigger number can take more bytes and damage the next record.

Step 5: Code:
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)


Step 6: Quick check:
Charges 5000 became 5500 and charges 3000 became 3500 in my test run.

Final Answer:
Read all records, add 500 to the fourth field of each, and write them back to the file in 'wb' mode.
Was this answer helpful?
0