PCAP Exceptions and File I/O Practice Question
A production script opens a configuration file with `open('config.ini', 'r')`. During a recent maintenance window, the file was replaced by a directory of the same name. The script now terminates with `IsADirectoryError`. The developer wants to catch this specific condition separately from a missing file and log a distinct message. Which `except` clause should be added?
⚠ Common exam trap
The trap here is assuming that any file-access problem surfaces as FileNotFoundError or the generic OSError alias IOError, when a path that exists as a directory raises the distinct IsADirectoryError subclass.
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
✓
except IsADirectoryError as e:
The directory-replacement scenario produces IsADirectoryError, a specific subclass of OSError raised when a path that should be a regular file resolves to a directory. Naming that exception in its own except clause isolates the condition and permits a tailored log message, while a separate FileNotFoundError clause can still handle a genuinely missing path. Broad parent handlers would merge the two cases.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
except FileNotFoundError as e:
Why it's wrong here
FileNotFoundError is raised when the path does not exist at all, not when the path exists but is a directory. Since the configuration path still exists as a directory, the interpreter raises IsADirectoryError instead, so this clause would never execute in the described situation and the script would still terminate.
- ✗
except IOError as e:
Why it's wrong here
IOError is an alias of OSError in Python 3, meaning it is the same broad parent class. Catching it would also capture FileNotFoundError and any other OS-level failure, preventing the developer from isolating the directory-specific case. It is a plausible-sounding but overly general handler for this scenario.
- ✗
except OSError as e:
Why it's wrong here
OSError is the parent class of both FileNotFoundError and IsADirectoryError, so this clause would also intercept a missing file and collapse two distinct conditions into one handler. The scenario requires separate treatment for the directory case, so catching the broad parent defeats the stated goal of logging a distinct message for each failure mode.
- ✓
except IsADirectoryError as e:
Why this is correct
IsADirectoryError is raised when the operating system reports that a path expected to be a regular file is actually a directory. Catching it by name lets the script log a specific message for that condition while allowing FileNotFoundError to fall through to its own handler. This directly satisfies the requirement to distinguish the two failures.
Visual reference
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 and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Python Institute exam blueprint
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.