To determine the correct order of statements for file operations in Python, it is essential to follow the logical sequence of opening a file, performing the required operations (like writing), providing feedback if necessary, and finally closing the file. Here's a breakdown:
open()
function to create or open a file for writing. This step must come first to set up the file handler (f
) that is used in subsequent operations.write()
method is used after the file has been opened to write data into the file.close()
method is called to close the file securely. This ensures all resources are freed and data is properly saved.Applying this logical order to the given options, we find:
Therefore, the correct order of the statements is:
The correct order of the statements to write data to a file is (B), (A), (C), (D).
Additional Context:
Correct Answer: (2) (B), (A), (C), (D).
List-I | List-II |
(A) readline() | (I) Writes a sequence of strings to the file |
(B) writelines() | (II) Reads a single line from the file |
(C) seek() | (III) Force any buffered output to be written to the file |
(D) flush() | (IV) Moves the file pointer to the specified position |