Skip to main contentSkip to solution

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

✅ Correct Option: 1

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.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question