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.
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.
continue means: stop processing this item and move on to the next loop iteration.
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.
- 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.
| Iteration | Line | Result | Statement reached |
|---|---|---|---|
| 1 | Subject: Meeting | Skipped | continue |
| 2 | From: alice@example.com | Processed | |
| 3 | Date: 2024-01-15 | Skipped | continue |
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.
for line in file:
if not line.startswith("From:"):
continue
print(line)- 1The loop receives one file line.
- 2The condition asks whether the line does not start with From:.
- 3If that is true, continue skips the remaining loop body.
- 4If that is false, execution reaches print.
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.
if not line.startswith("From:"):
continueThe condition and continue action occupy two lines.
if not line.startswith("From:"): continueThe same condition and action occupy one line.
Mistakes in Search Loops
Common mistake Treating continue as a way to exit the entire loop
What it looks likeA learner expects continue to stop the file-reading loop permanently.
Why it failscontinue skips only the rest of the current iteration and then begins the next iteration.
Do this insteadUse 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 likeThe loop processes a line before checking whether it is uninteresting.
Why it failsCode before continue has already executed, so the line was not filtered before processing.
Do this insteadPlace the if not condition: continue test before the statements that should run only for interesting lines.
Common mistake Reversing the condition
What it looks likeThe loop skips lines that start with From: instead of skipping lines that do not start with From:.
Why it failsThe condition determines which lines reach continue. Reversing it changes which lines are discarded.
Do this insteadRead the condition as a sentence: if this line is uninteresting, continue.
Common mistake Assuming the one-line form changes the behavior
What it looks likeA learner thinks if not condition: continue behaves differently from the indented two-line version.
Why it failsThe contracted form places the same action on the same line; it does not change the operation.
Do this insteadTreat the contracted and multi-line forms as equivalent forms of the same if statement.
Practice the Trace
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?
Reveal 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
- 1continue skips the rest of the current loop iteration and starts the next one.
- 2The pattern if not condition: continue filters out uninteresting file lines before later processing.
- 3Only lines that do not trigger continue reach the statements below it.
- 4The contracted if form places the condition and action on one line and functions like the equivalent multi-line form.
- 5Tracing 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.