Concepts / Reading Files Line by Line

Reading Files Line by Line

The continue statement skips the rest of the current loop iteration and jumps back to the loop start, enabling efficient filtering of uninteresting data.

  • 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.

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

line enters loopskip condition truejump to loop startline is interestingRead lineTest conditioncontinueSkip remaining bodyNext iterationProcess line
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

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.

condition truenext iterationcondition falsenext iterationcondition trueSubject: Meetingdoes not start with From:continueprint skippedFrom:alice@example.comstarts with From:print lineline is processedDate: 2024-01-15does not start with From:continueprint skipped
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.

for line in file: if not line.startswith("From:"): continue print(line)

test linenot condition is truenot condition is falseFile linenot conditioncontinueuninteresting lineLater statementsinteresting line
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.

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

Mistakes in Search Loops

  • Treating continue as a way to exit the entire loop

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

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

  • Putting processing code before the skip condition

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

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

  • Reversing the condition

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

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

  • Assuming the one-line form changes the behavior

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

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

Key Takeaways

  • 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.