- A
Use a try-except block catching FileNotFoundError
EAFP; clean and recommended for this scenario.
- B
Use a try-except block catching OSError
Why wrong: Too broad; catches more than necessary.
- C
Use os.path.exists to check, then open if it exists
Why wrong: LBYL approach; prone to race conditions and less Pythonic.
- D
Use an if statement to check file size
Why wrong: Does not handle missing file; incorrect logic.
Quick Answer
The correct approach is to use a try-except block catching FileNotFoundError, as this directly handles the specific exception raised when a file does not exist. This is the most Pythonic method because it follows the EAFP (Easier to Ask for Forgiveness than Permission) principle, where you attempt the operation and gracefully handle the failure rather than checking conditions beforehand with cluttered if-statements. On the Certified Associate Python Programmer PCAP exam, this question tests your understanding of exception hierarchy and idiomatic error handling—a common trap is catching the overly broad Exception or OSError, which can mask unrelated bugs. Remember that FileNotFoundError is a subclass of OSError, so catching the specific type keeps your code precise and maintainable. A useful memory tip: think "EAFP first, LBYL last"—always prefer try-except for file operations over os.path.exists checks, as the file could be deleted between the check and the open call.
PCAP Exceptions and File I/O Practice Question
This PCAP practice question tests your understanding of exceptions and file i/o. This is a configuration task: choose the command set that satisfies every stated requirement. Small differences — like 'secret' vs 'password' or 'transport input ssh' vs 'all' — change whether the answer is correct. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.
A developer writes a script to read a configuration file that may not exist. The script should handle the error gracefully and continue. Which approach is most Pythonic?
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
Use a try-except block catching FileNotFoundError
Option A is correct because it directly catches the specific `FileNotFoundError` exception, which is a subclass of `OSError` and is raised when a file does not exist. This approach follows the Pythonic principle of EAFP (Easier to Ask for Forgiveness than Permission), allowing the script to attempt the operation and handle the failure gracefully without redundant checks.
Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Use a try-except block catching FileNotFoundError
Why this is correct
EAFP; clean and recommended for this scenario.
Related concept
Read the scenario before looking for a memorised answer.
- ✗
Use a try-except block catching OSError
Why it's wrong here
Too broad; catches more than necessary.
- ✗
Use os.path.exists to check, then open if it exists
Why it's wrong here
LBYL approach; prone to race conditions and less Pythonic.
- ✗
Use an if statement to check file size
Why it's wrong here
Does not handle missing file; incorrect logic.
Common exam traps
Common exam trap: answer the scenario, not the keyword
Python Institute often tests the distinction between catching a specific exception (`FileNotFoundError`) versus a broader parent exception (`OSError`), and the trap here is that candidates may choose the broader catch thinking it is safer, without realizing it can mask other critical errors.
Detailed technical explanation
How to think about this question
In Python, `FileNotFoundError` is a subclass of `OSError` (specifically `OSError` with errno 2, `ENOENT`), introduced in Python 3.3 to provide more granular exception handling for file operations. The EAFP style is preferred in Python because it avoids race conditions inherent in LBYL checks and leads to cleaner, more readable code, especially in concurrent or multi-threaded environments where file system state can change between operations.
KKey Concepts to Remember
- Read the scenario before looking for a memorised answer.
- Find the constraint that changes the correct option.
- Eliminate answers that are true in general but not in this case.
TExam Day Tips
- Watch for words such as best, first, most likely and least administrative effort.
- Review why wrong options are wrong, not only why the correct option is correct.
Key takeaway
Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
Real-world example
How this comes up in practice
A company's IT admin needs to give a contractor read-only access to production logs without sharing account credentials. Using role-based access control (RBAC) and temporary scoped permissions — not a permanent shared password — is the correct pattern. Questions like this test whether you can apply least-privilege access across cloud identity services.
What to study next
Got this wrong? Here's your next step.
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
- →
Exceptions and File I/O — study guide chapter
Learn the concepts, then practise the questions
- →
Exceptions and File I/O practice questions
Targeted practice on this topic area only
- →
All PCAP questions
511 questions across all exam domains
- →
Certified Associate Python Programmer PCAP study guide
Full concept coverage aligned to exam objectives
- →
PCAP practice test guide
How to use practice tests most effectively before exam day
Related practice questions
Related PCAP practice-question pages
Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.
Modules and Packages practice questions
Practise PCAP questions linked to Modules and Packages.
Strings practice questions
Practise PCAP questions linked to Strings.
Object-Oriented Programming practice questions
Practise PCAP questions linked to Object-Oriented Programming.
Exceptions and File I/O practice questions
Practise PCAP questions linked to Exceptions and File I/O.
PCAP fundamentals practice questions
Practise PCAP questions linked to PCAP fundamentals.
PCAP scenario practice questions
Practise PCAP questions linked to PCAP scenario.
PCAP troubleshooting practice questions
Practise PCAP questions linked to PCAP troubleshooting.
Practice this exam
Start a free PCAP practice session
Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.
FAQ
Questions learners often ask
What does this PCAP question test?
Exceptions and File I/O — This question tests Exceptions and File I/O — Read the scenario before looking for a memorised answer..
What is the correct answer to this question?
The correct answer is: Use a try-except block catching FileNotFoundError — Option A is correct because it directly catches the specific `FileNotFoundError` exception, which is a subclass of `OSError` and is raised when a file does not exist. This approach follows the Pythonic principle of EAFP (Easier to Ask for Forgiveness than Permission), allowing the script to attempt the operation and handle the failure gracefully without redundant checks.
What should I do if I get this PCAP question wrong?
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
What is the key concept behind this question?
Read the scenario before looking for a memorised answer.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Same concept, more angles
3 more ways this is tested on PCAP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. A developer writes a script that reads a configuration file. If the file does not exist, the program should print an error and continue. Which code snippet correctly implements this behavior?
easy- ✓ A.try: open('config.txt') except FileNotFoundError: print('File not found')
- B.f = open('config.txt', 'r') if not f: print('File not found')
- C.try: f = open('config.txt') except: print('File not found')
- D.try: with open('config.txt') as f: pass except Exception: print('File not found')
Why A: Option A is correct because it uses a `try` block to attempt opening the file and catches only `FileNotFoundError`, which is the specific exception raised when the file does not exist. This allows the program to print an error and continue execution without crashing, precisely matching the requirement.
Variation 2. A developer writes a function that reads a configuration file and returns its contents as a string. The file might not exist. Which exception should be caught to handle a missing file?
easy- ✓ A.FileNotFoundError
- B.PermissionError
- C.IOError
- D.OSError
Why A: Option A is correct because `FileNotFoundError` is a built-in exception in Python that is raised when a file or directory is requested but does not exist. In Python 3, file-related I/O errors are organized under `OSError` with specific subclasses, and `FileNotFoundError` is the precise exception for a missing file, making it the most appropriate catch for this scenario.
Variation 3. A developer writes a function that reads a file and processes its content. The function should handle the case where the file does not exist without catching other I/O errors. Which exception should be caught?
easy- A.PermissionError
- B.IOError
- C.OSError
- ✓ D.FileNotFoundError
Why D: Option D is correct because `FileNotFoundError` is a specific subclass of `OSError` that is raised exactly when a file or directory is requested but does not exist. By catching only `FileNotFoundError`, the function handles the missing-file scenario without masking other I/O errors such as permission issues or disk failures, which is the precise requirement stated in the question.
Last reviewed: Jun 30, 2026
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.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.