Concepts / Reading Files Line by Line

Reading Files Line by Line

A search loop can test each line, skip lines that are uninteresting, and allow matching lines to reach the processing code below.

  • Programming

A Filtered Search

When a program reads a file line by line, it may need to process only lines that match a particular condition. A search loop can test each line, skip lines that are uninteresting, and allow matching lines to reach the processing code below. The continue statement is the control-flow tool that performs this skip.

Key point

continue means: stop processing this item and move on to the next loop iteration.

Read lineTest conditioncontinueSkip remaining bodyNext iterationProcess lineline enters loopskip condition truejump to loop startline is interesting
What happens next when continue is reached?

What continue Changes

When Python encounters continue inside a loop, it immediately stops executing the rest of the current iteration. Execution jumps back to the top of the loop, where the next iteration begins. Any statements below continue are skipped for that item. This differs from break: continue moves to the next iteration, while break exits the loop entirely.

python
Notes on the code
  • Each iteration supplies one line from the file.
  • Lines that do not start with From: execute continue.
  • Only lines that pass the condition reach print.

In this search loop, the condition checks whether a line does not start with From:. Such a line is treated as uninteresting and is skipped. A line that does start with From: does not execute continue, so it reaches the print statement.

Tracing Three Lines

Use a small input to make the control flow visible. Imagine that the file contains these three lines: Subject: Meeting, From: alice@example.com, and Date: 2024-01-15. The loop examines them in order. The first and third lines do not begin with From:, so they are skipped. The middle line begins with From:, so it reaches the print statement.

Subject: Meetingdoes not start with From:continueprint skippedFrom:alice@example.comstarts with From:print lineline is processedDate: 2024-01-15does not start with From:continueprint skippedcondition truenext iterationcondition falsenext iterationcondition true
Which lines are skipped, which are processed, and what is the execution state after each iteration?
IterationLineResultStatement reached
1Subject: MeetingSkippedcontinue
2From: alice@example.comProcessedprint
3Date: 2024-01-15Skippedcontinue

The loop processes only the line that begins with From:.

Filtering with Inverted Logic

A search loop is often clearer when it identifies the lines to skip rather than describing all the work to perform. The pattern if not condition: continue reverses the useful condition. If the desired condition is that a line starts with From:, then not line.startswith("From:") identifies every line that should be discarded.

Code walkthrough
for line in file:
    if not line.startswith("From:"):
        continue
    print(line)
  1. 1
    The loop receives one file line.
  2. 2
    The condition asks whether the line does not start with From:.
  3. 3
    If that is true, continue skips the remaining loop body.
  4. 4
    If that is false, execution reaches print.
File linenot conditioncontinueuninteresting lineLater statementsinteresting linetest linenot condition is truenot condition is false
How does the condition separate uninteresting lines from lines that continue through the loop body?

Contracted If Syntax

Python allows an if statement and its action to appear on one line. This is called the contracted form in the source material. In a search loop, it places the skip condition and continue action together, which keeps the filtering rule compact.

Before
if not line.startswith("From:"):
    continue

The condition and continue action occupy two lines.

After
if not line.startswith("From:"): continue

The same condition and action occupy one line.

ifkeywordnot conditiontest:separatorcontinuesame-line actionIndented continuemulti-line actioncontracted formexpanded form
How does the one-line form correspond to the expanded multi-line if statement?

Mistakes in Search Loops

  • Common mistake

    Treating continue as a way to exit the entire loop

    What it looks like

    A learner expects continue to stop the file-reading loop permanently.

    Why it fails

    continue skips only the rest of the current iteration and then begins the next iteration.

    Do this instead

    Use continue to discard one line. Remember that break, not continue, exits the loop entirely.

  • Common mistake

    Putting processing code before the skip condition

    What it looks like

    The loop processes a line before checking whether it is uninteresting.

    Why it fails

    Code before continue has already executed, so the line was not filtered before processing.

    Do this instead

    Place the if not condition: continue test before the statements that should run only for interesting lines.

  • Common mistake

    Reversing the condition

    What it looks like

    The loop skips lines that start with From: instead of skipping lines that do not start with From:.

    Why it fails

    The condition determines which lines reach continue. Reversing it changes which lines are discarded.

    Do this instead

    Read the condition as a sentence: if this line is uninteresting, continue.

  • Common mistake

    Assuming the one-line form changes the behavior

    What it looks like

    A learner thinks if not condition: continue behaves differently from the indented two-line version.

    Why it fails

    The contracted form places the same action on the same line; it does not change the operation.

    Do this instead

    Treat the contracted and multi-line forms as equivalent forms of the same if statement.

Practice the Trace

MEDIUM

Trace this search loop against the three sample lines from the example. For each line, decide whether continue runs or print runs, and explain why.

Hints
  • Check whether the line starts with From:.
  • A line that does not start with From: meets the not condition.
  • After continue, the loop begins its next iteration.

What do you think happens?

Which sample line reaches print: Subject: Meeting, From: alice@example.com, or Date: 2024-01-15?

  • Subject: Meeting

  • From: alice@example.com

  • Date: 2024-01-15

Reveal answer
Answer:

From: alice@example.com

The other two lines do not start with From:, so continue skips the rest of their iterations. The From: line passes the filter and reaches print.

Key Takeaways

  1. 1
    continue skips the rest of the current loop iteration and starts the next one.
  2. 2
    The pattern if not condition: continue filters out uninteresting file lines before later processing.
  3. 3
    Only lines that do not trigger continue reach the statements below it.
  4. 4
    The contracted if form places the condition and action on one line and functions like the equivalent multi-line form.
  5. 5
    Tracing each input line makes it possible to verify exactly which lines are skipped and which are processed.

At a glance

  • continue moves execution directly to the next loop iteration.
  • A search loop can skip uninteresting lines with the if not condition: continue pattern.
  • The statements after continue run only for lines that pass the filter.
  • The contracted if syntax is a compact equivalent of the indented multi-line form.
  • A line-by-line trace reveals the loop's execution state for every file line.

Related concepts

More concepts starting with R