PCAP Exceptions and File I/O Practice Question
A developer needs to write binary data to a file. Which file mode should be used to open the file for writing in binary mode without truncating it if it already exists?
⚠ Common exam trap
Python Institute often tests the distinction between 'w' (truncate) and 'a' (append) modes, trapping candidates who assume 'wb' is the only way to write binary data without considering preservation of existing content.
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
✓
'ab'
('ab') is correct because the 'a' mode opens the file for appending, which writes data at the end without truncating the existing content, and adding 'b' specifies binary mode. This allows binary data to be written to a file that already exists without losing its current contents.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
'wb'
Why it's wrong here
While 'wb' does permit writing binary data, opening with this mode destroys any existing content by truncating the file to zero length before the first write. If the developer needs to preserve existing file contents and simply append new binary data, this mode is inappropriate. It is only usable when overwriting the entire file from the beginning is acceptable.
- ✗
'rb'
Why it's wrong here
The 'rb' mode opens the file for binary read-only access; any attempt to write produces an io.UnsupportedOperation error. It does not create a file if it does not exist and simply fails with FileNotFoundError. Therefore, it cannot satisfy the requirement to write binary data.
- ✓
'ab'
Why this is correct
'ab' opens a file for binary append: if the file exists, the file pointer is positioned at the end so all writes are appended after existing data, and the original content is never truncated. If the file does not exist, a new empty file is created automatically. This makes 'ab' the appropriate choice when writing binary data without destroying prior content.
- ✗
'wb+'
Why it's wrong here
Although 'wb+' permits both binary reads and writes, it opens by truncating any existing file to zero length, just like 'wb'. This means any previous data is lost immediately, making it unsuitable if preservation or appending is intended. The extra read permission does not compensate for the destructive truncation.
Go deeper
Related to this question
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 →
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.