Courseiva
Exceptions and File I/O →mediumMultiple Choice

PCAP Exceptions and File I/O Practice Question

Exhibit

Config file: /etc/myapp/config.ini
[files]
input = /var/data/in.csv
output = /var/data/out.csv

Code:
config = configparser.ConfigParser()
config.read('/etc/myapp/config.ini')
with open(config['files']['input'], 'r') as f:
    lines = f.readlines()
    print(len(lines))

Refer to the exhibit. If /var/data/in.csv does not exist, what exception is raised?

⚠ Common exam trap

A common trap in Python exams is confusing FileNotFoundError with PermissionError. When a file does not exist, Python raises FileNotFoundError, not PermissionError. Always verify the file's existence before attempting to open 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

✓

FileNotFoundError

When a file does not exist and you attempt to open it for reading using the built-in `open()` function, Python raises a `FileNotFoundError`. This is a subclass of `OSError` and is the standard exception for missing files in Python 3. The question explicitly states that `/var/data/in.csv` does not exist, so the correct exception is `FileNotFoundError`.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    configparser.NoSectionError

    Why it's wrong here

    A missing file raises FileNotFoundError when opened; configparser.NoSectionError arises only when parsing a configuration file that lacks the requested section header. It is tempting because the exhibit parses configuration, and would be correct if the config file existed but omitted the named section.

  • ✗

    PermissionError

    Why it's wrong here

    PermissionError requires the file to exist but deny the process read access; a nonexistent path yields FileNotFoundError regardless of permissions. It is tempting because both are OSError subclasses raised during file access, and would be correct if the file existed with restrictive permissions.

  • ✗

    IndexError

    Why it's wrong here

    IndexError occurs when a sequence is indexed beyond its bounds, whereas opening a nonexistent path raises FileNotFoundError before any indexing happens. It is tempting because the script later indexes parsed rows, and would be correct if the file existed but contained fewer records than the code expects.

  • ✓

    FileNotFoundError

    Why this is correct

    Opening /var/data/in.csv with the built-in open() function raises FileNotFoundError when the path does not exist, satisfying the stem's missing-file constraint. This OSError subclass carries errno ENOENT and is raised before any read occurs, so no other exception type applies here.

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 →

How Courseiva writes practice questions · Editorial policy

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.