Skip to content
Question

Q.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 :

(i) Append() – To input the data of a Resource Person and write it in the file RESOURCES.DAT.
(ii) Update() – To increase the Charges of each resource person by 500.
CBSECBSE Class XII Board 2026Subjective· 5mImportance★★★★★
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

You need to write two Python functions: Append() to add a new resource person's record to a binary file, and Update() to increase every resource person's charges by 500 rupees.

Binary files in Python store data in a compact, machine-readable format rather than plain text. When you work with structured records like those of NextStep's resource persons, Python's pickle module becomes your tool of choice. It serializes Python objects (in this case, tuples) into a binary stream that can be written to a file and later read back exactly as they were.

The file RESOURCES.DAT holds tuples, each representing one resource person. The structure is fixed: an integer ID, a name string, an expertise area string, and an integer charge. Because it's a binary file, you cannot simply open it in a text editor and read it—you need Python's pickle.load() to deserialize the data and pickle.dump() to serialize and write it back.

The Append Function

The first task is straightforward. You want to accept input from the user for a new resource person and add that record to the end of the existing file. Opening a binary file in append mode ('ab') ensures that you don't overwrite existing data; instead, the new record is tacked on at the end.

Here's how Append() works:

import pickle

def Append():
    # Open the file in append-binary mode
    with open('RESOURCES.DAT', 'ab') as file:
        # Input the details
        r_id = int(input("Enter Resource Person's ID: "))
        r_name = input("Enter Resource Person's Name: ")
        r_expertise = input("Enter Area of Expertise: ")
        charges = int(input("Enter Charges per hour: "))
        
        # Create a tuple
        record = (r_id, r_name, r_expertise, charges)
        
        # Write the tuple to the file
        pickle.dump(record, file)
        print("Record appended successfully.")

The with statement handles file closing automatically, even if an error occurs. Each call to pickle.dump() writes one complete tuple to the file. Because the file is opened in append mode, repeated calls to Append() will keep adding records without disturbing earlier ones.

Note

If RESOURCES.DAT does not exist, opening it in 'ab' mode will create it. This is safe and convenient for the first run.

The Update Function

The second function is more involved. You cannot modify a binary file in place the way you might edit a text file line by line. Instead, the standard approach is to:

  • Read all existing records into a Python list.
  • Modify the records in memory (in this case, increase each person's charges by 500).
  • Write the entire updated list back to the file, overwriting the old contents.

Here's the Update() function:

import pickle

def Update():
    # Step 1: Read all records
    records = []
    try:
        with open('RESOURCES.DAT', 'rb') as file:
            while True:
                try:
                    record = pickle.load(file)
                    records.append(record)
                except EOFError:
                    # End of file reached
                    break
    except FileNotFoundError:
        print("File not found. No records to update.")
        return
    
    # Step 2: Update each record
    updated_records = []
    for record in records:
        r_id, r_name, r_expertise, charges = record
        new_charges = charges + 500
        updated_record = (r_id, r_name, r_expertise, new_charges)
        updated_records.append(updated_record)
    
    # Step 3: Write all updated records back
    with open('RESOURCES.DAT', 'wb') as file:
        for record in updated_records:
            pickle.dump(record, file)
    
    print("All charges updated successfully.")
``` …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.