Text files are lines. Reading them one at a time keeps memory flat and lets you stop early, which a whole-file read cannot do.
What you will see
2
About 2 min of reading
Quick check
Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.
Try It
Walk lines and count them.
1
Split three rows.
text = "a\nb\nc"
print(len(text.splitlines()))
Before you run it, say out loud what it will print. Then open the answer.
What it prints
3
2
Lengths with and without the ending.
line = "hello\n"
print(len(line), len(line.strip()))
Before you run it, say out loud what it will print. Then open the answer.
What it prints
6 5
3
Rows with a trailing ending.
rows = "a\nb\nc\n"
print(len(rows.splitlines()))
Before you run it, say out loud what it will print. Then open the answer.
What it prints
3
4
Blank lines filtered.
lines = ["a", "", "b"]
kept = [x for x in lines if x]
print(len(kept))
Before you run it, say out loud what it will print. Then open the answer.
What it prints
2
Put it in order
Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.
Data Flow
A file iterated gives one line at a time. Each line still carries its ending, so strip before comparing.
1open the file
↓
2iterate lines
↓
3strip each one
↓
4stop when done
Line by line
Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.
text = "a\nb\nc"sets nothing on screen
print(len(text.splitlines()))3
Why this worksStreaming keeps memory flat
Iterating a file holds one line at a time. Reading it all holds the whole file, which is the difference at a million lines.
The ending is part of the line
A line includes its newline until you strip it. Comparing without stripping fails on every row.
Examples
Loop over lines
withopen("notes.txt") as f:
for line in f:
print(line.strip())
Iterating a file yields one line at a time. Memory stays small whatever the size.
Lines into a list
text = "a\nb\nc"
lines = text.splitlines()
print(len(lines))
splitlines cuts on any line ending. The count is the number of rows.
Strip the ending
line = "hello\n"print(len(line), len(line.strip()))
Every line carries its ending. Strip removes it before you compare.
Stop at the first hit
for line in ["x", "find", "y"]:
if line == "find":
print(line)
break
A loop can stop early. A whole-file read already paid for every byte.
Count the rows
rows = "a\nb\nc\n"print(len(rows.splitlines()))
A trailing ending does not add an empty row. The count stays three.
Skip blank lines
lines = ["a", "", "b"]
kept = [x for x in lines if x]
print(len(kept))
A blank line is a real line with nothing in it. Filter it when you want rows only.
One pass, two counts
lines = ["ok", "bad", "ok"]
bad = sum(1for x in lines if x == "bad")
print(len(lines), bad)
Count while you read. Storing the lines to count later costs memory.
Words this lesson uses
Each one is defined here on this site. Open the entry for the full definition and the other words it points at.