Courseiva
Exceptions and File I/O →mediumMultiple Choice

PCAP Exceptions and File I/O Practice Question

A Python script that processes log files uses the following code:

with open('log.txt', 'r') as f:

lines = f.readlines()

for line in lines:
        # process

What is a potential inefficiency in this code?

⚠ Common exam trap

Python Institute often tests the misconception that `readlines()` is the standard or recommended way to read a file line by line, when in fact the file object itself is an iterator that should be used for large files to avoid memory bloat.

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

✓

Reading the entire file into memory may be wasteful for large files.

`readlines()` loads the entire file into memory as a list of strings. For large log files, this can consume significant memory and cause performance degradation or even memory errors. A more memory-efficient approach is to iterate directly over the file object (e.g., `for line in f:`), which reads one line at a time from disk.

Answer analysis

Option-by-option breakdown

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

  • ✗

    The file is not properly closed after processing.

    Why it's wrong here

    The code uses a `with` statement, which is a context manager that guarantees the file is properly closed when the block exits. Even if an exception occurs during processing, the `__exit__` method is invoked and `file.close()` is called automatically, releasing the underlying file descriptor. Therefore, the file is never left open after processing, making this concern unfounded.

  • ✓

    Reading the entire file into memory may be wasteful for large files.

    Why this is correct

    This is the correct statement. Calling `readlines()` reads every line of the file into a list of strings, meaning the entire file content is loaded into memory at once. For large log files, this can consume a massive amount of RAM, potentially causing the program to slow down or crash with a MemoryError. A more memory-efficient approach is to iterate directly over the file object (`for line in f:`), which reads one line at a time lazily, keeping the memory footprint low regardless of file size.

  • ✗

    Using readlines() is the most efficient way to iterate over lines.

    Why it's wrong here

    `readlines()` is actually one of the least memory-efficient ways to iterate over lines. It materializes a list containing every line of the file, requiring memory proportional to the entire file size, whereas a standard `for line in file:` loop reads a single line at a time using the file object's internal iterator. The lazy iteration approach avoids loading all lines into memory, making it more efficient in both memory usage and often in speed for large files.

  • ✗

    The file should be opened in binary mode for better performance.

    Why it's wrong here

    Opening the file in binary mode (`'rb'`) would return bytes objects instead of strings, requiring manual decoding for text processing and complicating line splitting, while providing no performance benefit for this use case. The performance bottleneck here is reading the entire file into memory with `readlines()`, not the file mode. Text mode is appropriate for text log files, and switching to binary mode would not reduce memory consumption or make iteration faster.

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.