PCEP Computer Programming and Python Fundamentals Practice Question
You are a developer in a company that runs a Python script daily to generate reports. The script uses the os module to list files in a directory and process each. Recently, after a server migration, the script fails with 'PermissionError: [Errno 13] Permission denied'. The script runs under a service account that has read/write access to most folders, but the migration changed the permissions on certain subdirectories. The error is intermittent, occurring only for some files. You need to fix the script to continue processing other files even if one fails. Which approach should you take?
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 inside the loop to catch PermissionError for each file and continue.
By placing a try-except block inside the loop that catches PermissionError specifically, the script can skip the problematic file and continue processing the remaining files. This approach handles the intermittent permission errors gracefully without halting the entire script. Option B is wrong because catching all exceptions silently would mask other critical errors (e.g., bugs in the processing logic). Option C is wrong because os.access() checks may not fully reflect actual runtime permissions due to race conditions and platform-specific behavior, and it requires additional calls that could still fail. Option D is wrong because it relies on external action and does not address the need for the script to be resilient to such errors.
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 inside the loop to catch PermissionError for each file and continue.
Why this is correct
Selective exception handling allows graceful continuation.
- ✗
Wrap the entire processing loop in a try-except that catches all exceptions and passes silently.
Why it's wrong here
Silently ignoring all exceptions hides real errors (e.g., data corruption).
- ✗
Before processing each file, use os.access() to check permissions and skip if not accessible.
Why it's wrong here
os.access() is not reliable due to race conditions and complex permission models.
- ✗
Ask the server administrator to grant full permissions to the service account on all directories.
Why it's wrong here
This is a temporary fix and not a robust programming solution.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCEP question from scratch — 498 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 PCEP 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 PCEP exam.