Sequence the steps to append data to an existing file and then read the entire file:
(A) Open the file in a+ mode.
(B) Use the read() method to output the contents.
(C) Use the write() or writelines() method to append data.
(D) Seek to the beginning of the file.
Sequence the steps to append data to an existing file and then read the entire file:
(A) Open the file in a+ mode.
(B) Use the read() method to output the contents.
(C) Use the write() or writelines() method to append data.
(D) Seek to the beginning of the file.
Solution
In a+ mode the file opens for both appending and reading, but the file pointer starts at the end. So you write first, then seek(0) to move back to the start, and only then call read() to get the whole file.
(A) Open the file in a+ mode.
(C) Use write() or writelines() to append data.
(D) Seek to the beginning with seek(0).
(B) Use read() to output the contents.
Option 2: read() before seek(0) starts from the end of the file, so it returns an empty string.
Extra point: in a+ mode every write goes to the end of the file, even if you call seek() before writing.
Related questions:
2025: 3 June Shift 2
2025: 27 May Shift 2
2026: 23 May Shift 2