The following Python program is designed to update the phone number of a…

PYQ EMRS TGT/PGT (ESSE) 2025

The following Python program is designed to update the phone number of a specific person in a binary file named directory.dat. The file stores records as lists in the format [Name, Phone]. The function searches for a record matching Name. If found, it updates the phone number and saves the changes back to the file.

import pickle

def Update(Name, New_Phone):
    Found = False
    Records = []
    try:
        ______ (A) ______ as File:
            while True:
                try:
                    Rec = ______ (B) ______
                    if Rec[0] == Name:
                        Rec[1] = New_Phone
                        Found = True
                    Records.append(Rec)
                except EOFError:
                    break

        if Found:
            ______ (C) ______ as File:
                for Rec in Records:
                    ______ (D) ______
            print("Update successful!")
        else:
            print("Record not found.")

    except FileNotFoundError:
        print("File not found.")

Questions:

(A) Write the complete open statement with appropriate file mode for Blank (A) that allows reading a binary file.

(B) Provide the method call for Blank (B) to read and deserialize a single record from the file object File.

(C) Write the complete 'open' statement with appropriate file mode for Blank (C) that allows writing the updated data into the binary file.

(D) Write the code for Blank (D) to write the updated record Rec into the file object File.

Attempted by 54 students.

Sign up free to check your answer

Sign up free

Explore the full course: Emrs Cs Tier 2

Loading lesson…