PCAP Exceptions and File I/O Practice Question
Which of the following is the correct way to open a file for writing in binary mode?
⚠ Common exam trap
Python Institute often tests the strict ordering of mode characters in the open() function, expecting candidates to know that 'b' must always follow the read/write/append character, not precede it.
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
✓
open('data.dat', 'wb')
The mode string 'wb' specifies both write ('w') and binary ('b') mode, which is the proper way to open a file for writing binary data in Python. The order of characters in the mode string is fixed: the file mode character ('r', 'w', 'a', etc.) must come first, followed by the optional 'b' for binary 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.
- ✓
open('data.dat', 'wb')
Why this is correct
The mode string 'wb' is the correct Python file mode for binary writing. The first character 'w' specifies the operation (write), and the second character 'b' explicitly selects binary mode rather than text mode. This mode both creates the file if it does not exist and truncates it to zero length if it does, allowing you to write raw bytes without any encoding or newline translation.
- ✗
open('data.dat', 'bw')
Why it's wrong here
The mode string 'bw' is invalid because Python requires the operation character (r, w, a, x, or +) to be the first character of the mode string, with an optional second character for the data type ('b' or 't'). Placing 'b' first is not a recognized operation, so attempting to open a file with 'bw' raises a ValueError rather than opening anything. The order of characters in the mode string is fixed by the open() specification, not a free choice.
- ✗
open('data.dat', 'br')
Why it's wrong here
The mode 'br' is a valid mode string, but it designates binary reading, not writing. Because 'r' stands for read, the file is only opened for reading binary data; any attempt to write will raise an OSError or io.UnsupportedOperation. Additionally, if the file does not already exist, this mode will raise FileNotFoundError instead of creating it, so it cannot serve the intended purpose of writing a binary file.
- ✗
open('data.dat', 'w')
Why it's wrong here
The mode 'w' opens the file for text writing, not binary writing. Without the 'b' flag, the file is treated as a text file, which means Python applies character encoding (e.g., UTF-8) and newline translation when writing. Consequently, attempting to write bytes objects will raise a TypeError, and any raw binary content will not be preserved correctly, potentially corrupting non-text data such as images or archived files.
Go deeper
Related to this question
About these practice questions
One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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.