Carefully analyze the following Python program. The purpose of the program is…

PYQ EMRS TGT/PGT (ESSE) 2025

Carefully analyze the following Python program. The purpose of the program is to replace all occurrences of the word "Bombay" with the word "Mumbai" in a text file named "maharashtra.txt". However, certain parts of the code are missing and have been labeled as Task A, Task B, Task C and Task D. Complete the missing statements so that the program functions correctly. The file must be opened only once in a mode that allows both reading and writing.

try:
    _____________ as File:   # Task A
        Text = ______                              # Task B
        NewText = Text.replace("Bombay", "Mumbai")
        File.______                                # Task C
        ______                                     # Task D
        File.truncate()
except:
    print('File I/O Error!')

Questions:

(A) Write the missing statement for Task A to open the file in both read and write mode.

(B) Write the missing statement for Task B to read the entire content of the file and store it in the identifier Text.

(C) Write the missing statement for Task C to move the file pointer to the beginning of the file.

(D) Write the missing statement for Task D to write the modified content stored in NewText back into the file.

Attempted by 88 students.

Sign up free to check your answer

Sign up free

Explore the full course: Emrs Cs Tier 2

Loading lesson…