English

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 - Computer Science (Python)

Advertisements
Advertisements

Question

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:
  1. Append() – To input the data of a Resource Person and write it in the file RESOURCES.DAT.
  2. Update() – To increase the Charges of each resource person by 500.
Code Writing
Advertisements

Solution

  1. import pickle
    
    def Append():
        fobj = open('RESOURCES.DAT', 'ab')
        r_id = int(input('Enter RID: '))
        r_name = input('Enter Name: ')
        r_exp = input('Enter Expertise: ')
        charges = float(input('Enter Charges: '))
        
        # Store data as a tuple
        rec = (r_id, r_name, r_exp, charges)
        pickle.dump(rec, fobj)
        fobj.close()
  2. def Update():
        fobj = open('RESOURCES.DAT', 'rb+')
        try:
            while True:
                pos = fobj.tell()           # Save current pointer position
                rec = list(pickle.load(fobj)) # Convert tuple to list to edit
                rec[3] += 500               # Increase Charges (index 3) by 500
                
                fobj.seek(pos)              # Move pointer back to record start
                pickle.dump(tuple(rec), fobj) # Save as tuple
        except EOFError:
            fobj.close()
shaalaa.com
  Is there an error in this question or solution?
2025-2026 (March) Set 4
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×