- A
Use os.path.exists before opening the file, and if it returns False, log and skip.
Why wrong: LBYL approach; race condition risk and not robust for network drives.
- B
Use a try-except block catching OSError, with a retry loop that logs the error and continues after max retries.
Correctly catches I/O errors, implements retry, and logs specifically.
- C
Use a try-except block catching Exception, with a retry loop and logging.
Why wrong: Too broad; would catch programming errors that should fail fast.
- D
Use a with statement; if an exception occurs, call the function recursively up to 3 times.
Why wrong: Recursive approach risks stack overflow and is not clean.
Quick Answer
The correct approach is to use a try-except block catching OSError, with a retry loop that logs the error and continues after max retries. This is correct because OSError is the parent class for file-related exceptions like FileNotFoundError, so it specifically handles the network drive issue without masking unrelated programming errors such as NameError, which a bare except clause would wrongly suppress. On the PCAP exam, this tests your understanding of exception hierarchy and robust error handling—a common trap is using a bare except or catching Exception too broadly, which hides bugs. The requirement to retry file open with OSError maximum retries of three and a two-second delay is a classic pattern for transient network failures; after exhausting attempts, the loop naturally skips the file. A helpful memory tip: “OSError for files, bare except for trials”—always catch the specific parent class for file operations to keep your debugging clear.
PCAP Exceptions and File I/O Practice Question
This PCAP practice question tests your understanding of exceptions and file i/o. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. 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 company runs a data processing pipeline that reads CSV files from a network drive. Occasionally, the network drive is unreachable causing FileNotFoundError. The current code uses a bare except clause that catches all exceptions, which also masks programming errors like NameError. The lead developer wants to implement a more robust exception handling strategy. The requirement is to log the specific error (including the filename) and retry the operation up to 3 times with a 2-second delay between retries. If after 3 attempts the file is still inaccessible, the pipeline should skip the file and continue with the next one. Which approach best meets these requirements?
Clue words in this question
Noticing these words before you look at the options changes how you read each choice.
Clue:
"best"Why it matters: Signals that multiple options may be partially correct. Choose the option that most directly solves the exact problem described, not the one that sounds most complete.
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 OSError, with a retry loop that logs the error and continues after max retries.
Option B is correct because it catches OSError, which is the parent class for file-related errors like FileNotFoundError, while still allowing programming errors like NameError to propagate. The retry loop with a 2-second delay and a maximum of 3 attempts satisfies the requirement, and after exhausting retries, the loop naturally continues to the next file, skipping the problematic one.
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 os.path.exists before opening the file, and if it returns False, log and skip.
Why it's wrong here
LBYL approach; race condition risk and not robust for network drives.
- ✓
Use a try-except block catching OSError, with a retry loop that logs the error and continues after max retries.
Why this is correct
Correctly catches I/O errors, implements retry, and logs specifically.
Clue confirmation
The clue word "best" in the question point toward this answer.
Related concept
Read the scenario before looking for a memorised answer.
- ✗
Use a try-except block catching Exception, with a retry loop and logging.
Why it's wrong here
Too broad; would catch programming errors that should fail fast.
- ✗
Use a with statement; if an exception occurs, call the function recursively up to 3 times.
Why it's wrong here
Recursive approach risks stack overflow and is not clean.
Common exam traps
Common exam trap: answer the scenario, not the keyword
Python Institute often tests the distinction between catching OSError (specific to file/OS errors) versus Exception (which catches everything including NameError), and candidates mistakenly choose the broader catch because they think 'all exceptions' includes file errors, ignoring the requirement to not mask programming errors.
Detailed technical explanation
How to think about this question
OSError is the base class for file I/O exceptions in Python, including FileNotFoundError, PermissionError, and IsADirectoryError. The retry loop with time.sleep(2) uses a blocking delay, which is acceptable for a pipeline that processes files sequentially; in a production system, exponential backoff might be preferred to avoid hammering a recovering network drive. The requirement to 'skip and continue' is naturally achieved by letting the loop proceed to the next iteration after the retry count is exhausted, without raising an exception.
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 OSError, with a retry loop that logs the error and continues after max retries. — Option B is correct because it catches OSError, which is the parent class for file-related errors like FileNotFoundError, while still allowing programming errors like NameError to propagate. The retry loop with a 2-second delay and a maximum of 3 attempts satisfies the requirement, and after exhausting retries, the loop naturally continues to the next file, skipping the problematic one.
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.
Are there clue words in this question I should notice?
Yes — watch for: "best". Signals that multiple options may be partially correct. Choose the option that most directly solves the exact problem described, not the one that sounds most complete.
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 →
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.