Which file mode in Python is primarily used to append new content to an…

2024

Which file mode in Python is primarily used to append new content to an existing file without deleting the previous data?

  1. A.

    r

  2. B.

    w

  3. C.

    x

  4. D.

    a

  5. E.

    rb

Attempted by 47 students.

Show answer & explanation

Correct answer: D

Concept: In Python, the file mode passed to open() decides how the stream is positioned and whether existing data is preserved. Append mode opens the file with the stream positioned at the end and never truncates existing content, so every write adds to what is already there.

Application: The mode that appends new content while keeping previous data is a. open(path, 'a') creates the file if it does not exist, places the file pointer at the end, and writes are added after the existing bytes — nothing already stored is lost.

Cross-check — why the other modes do not fit:

  • r opens for reading only; you cannot write through it.

  • w opens for writing but truncates the file to zero length first, destroying previous data.

  • x is exclusive creation; it raises FileExistsError if the file already exists, so it cannot add to an existing file.

  • rb reads the file in binary mode; it is read-only and cannot append.

Therefore append mode is the only one that adds new content without deleting what is already stored.

Explore the full course: Ibps So It Mains

Loading lesson…