Question:medium

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.

Show Hint

Always use pickle for reading/writing complex objects to binary files.
Handle EOFError to stop reading when end of file is reached.
When updating a binary file, read all records first, modify them, and then overwrite.
Use try-except blocks to handle missing files gracefully.
Updated On: Jan 14, 2026
Show Solution

Solution and Explanation

(i) Function to create binary file with passenger records:
import pickle

def Create():
    with open("PASSENGERS.DAT", "wb") as f:
        n = int(input("Enter number of passengers: "))
        for i in range(n):
            PNR = input("Enter PNR: ")
            PName = input("Enter 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)
Explanation:
- pickle.dump() serializes and writes each record (as a list) to the binary file. - User input is collected for each record and type-casted as necessary. - The file is opened in write-binary mode ("wb").
(ii) Function to search by DESTN and display matching records:
def SearchDestn(D):
    try:
        with open("PASSENGERS.DAT", "rb") as f:
            print("Passengers going to", D)
            while True:
                rec = pickle.load(f)
                if rec[3].lower() == D.lower():
                    print(rec)
    except EOFError:
        pass
    except FileNotFoundError:
        print("File not found.")
Explanation:
- Records are deserialized and read sequentially using pickle.load(). - The loop terminates upon reaching the end of the file (EOFError). - A case-insensitive comparison is performed on the destination field (index 3).
(iii) Function to update fare of all passengers by 5% and rewrite file:
def UpdateFare():
    updated = []
    try:
        with open("PASSENGERS.DAT", "rb") as f:
            while True:
                rec = pickle.load(f)
                rec[4] = round(rec[4] * 1.05, 2)
                updated.append(rec)
    except EOFError:
        pass
    except FileNotFoundError:
        print("File not found.")
        return

    with open("PASSENGERS.DAT", "wb") as f:
        for rec in updated:
            pickle.dump(rec, f)
Explanation:
- The fare of each record (index 4) is increased by 5%. - All modified records are temporarily stored in a list. - The file is then reopened in write-binary mode ("wb") to overwrite with the updated records. - round(..., 2) is used for formatting the fare to two decimal places.
Was this answer helpful?
0