Courseiva
Exceptions and File I/O →easyMultiple Choice

PCAP Exceptions and File I/O Practice Question

Which mode should be used when opening a file for writing such that new content is appended to the end without truncating existing content?

⚠ Common exam trap

Python Institute often tests the confusion between 'w' (which truncates) and 'a' (which appends), and candidates mistakenly choose 'w' thinking it simply writes without realizing it destroys existing data.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

'a'

('a') is correct because the 'a' mode opens a file for appending, which means new data is written to the end of the file without truncating any existing content. This is the standard behavior defined in Python's open() function for append mode.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    'x'

    Why it's wrong here

    The 'x' mode requests exclusive creation: it opens the file for writing, but only if the file does not already exist, and raises FileExistsError otherwise. This guarantees no existing data is touched, including appending, because the file must be newly created. Since the correct goal is to add data to an existing file, 'x' would fail outright rather than append.

  • ✗

    'r+'

    Why it's wrong here

    The 'r+' mode opens an existing file for both reading and writing with the file pointer positioned at the beginning; it does not automatically append and it fails if the file is missing. Any write operation starts at offset 0 and overwrites existing bytes, so it is not suitable for adding content to the end of a file. It also requires the file to exist, unlike append mode, which creates it if needed.

  • ✗

    'w'

    Why it's wrong here

    The 'w' mode opens the file for writing, but truncates it to zero length immediately upon opening, discarding all previous contents. While it does create the file if it does not exist, this destructive behavior is the opposite of appending; it replaces the entire file rather than preserving and extending it. Therefore, 'w' is wrong for any operation that must add data to existing content.

  • ✓

    'a'

    Why this is correct

    The 'a' (append) mode opens the file for writing and places the file pointer at the end of the existing content, so every write extends the file without overwriting or truncating it. If the file is missing, Python creates a new empty file, matching the requirement to write new data while preserving what is already there. This is the only standard write mode designed specifically for adding to the end of a file.

About these practice questions

Courseiva writes every PCAP question from scratch — 421 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.