Test Index
CBSE Class 12 Computer Science 2025 Paper Set 4
© examsnet.com
Question : 36 of 37
Marks:
+1,
-0
A file, PASSENGERS.DAT, stores the records of passengers using the following structure :[PNR, PName, BRDSTN, DESTN, FARE]where :PNR – Passenger Number (string type)PName – Passenger Name (string type)BRDSTN – Boarding Station Name (string type)DESTN – Destination Station Name (string type)FARE – Fare amount for the journey (float type)Write user defined functions in Python for the following tasks :(i) Create() – to input data for passengers and write it in the binary file PASSENGERS.DAT.(ii) SearchDestn(D) – to read contents from the file PASSENGERS.DAT and display the details of those Passengers whose DESTN matches with the value of D.(iii) UpdateFare() – to increase the fare of all passengers by 5% and rewrite the updated records into the file PASSENGERS.DAT.
Solution:
Concept:A binary file is handled with the pickle module: pickle.dump(obj, file) writes one object and pickle.load(file) reads the next object, raising EOFError at the end of the file. Here each passenger record is a list dumped one after another. Indexes: Rec[0] PNR, Rec[1] PName, Rec[2] BRDSTN, Rec[3] DESTN, Rec[4] FARE.(i) Create()Open the file in "ab" mode, input the five fields (FARE as float), form the list and dump it; repeat while the user wants more records.
import pickledef Create(): F = open("PASSENGERS.DAT", "ab") # append in binary mode while True: PNR = input("Enter PNR: ") PName = input("Enter Passenger Name: ") BRDSTN = input("Enter Boarding Station: ") DESTN = input("Enter Destination Station: ") FARE = float(input("Enter Fare: ")) Rec = [PNR, PName, BRDSTN, DESTN, FARE] pickle.dump(Rec, F) # write one record ch = input("More records (Y/N)? ") if ch.upper() != "Y": break F.close()(ii) SearchDestn(D)Open in "rb" mode, load records one by one until EOFError, and print a record when its DESTN equals D.import pickledef SearchDestn(D): F = open("PASSENGERS.DAT", "rb") # read binary mode found = False try: while True: Rec = pickle.load(F) # read next record if Rec[3] == D: # Rec[3] is DESTN print(Rec) found = True except EOFError: # end of file reached pass F.close() if not found: print("No passenger for destination", D)(iii) UpdateFare()Read all records into a list, multiply each FARE by 1.05 (a 5% increase), then open the file in "wb" mode and write all records back.import pickledef UpdateFare(): Records = [] F = open("PASSENGERS.DAT", "rb") # 1. read all records try: while True: Records.append(pickle.load(F)) except EOFError: pass F.close() for Rec in Records: # 2. increase fare by 5% Rec[4] = Rec[4] * 1.05 # Rec[4] is FARE F = open("PASSENGERS.DAT", "wb") # 3. rewrite the file for Rec in Records: pickle.dump(Rec, F) F.close()Example: the record ['P101', 'Ravi', 'Delhi', 'Agra', 800.0] becomes ['P101', 'Ravi', 'Delhi', 'Agra', 840.0] after UpdateFare() ().Key point: Use "ab" in Create() so old records are not erased ("wb" would empty the file). In UpdateFare() the file is opened in "wb" only after all records have been read into memory.© examsnet.com
Go to Question: