Master

A small file tool

A tool that walks files has to hold all of it at once: the walk, the read, the failure, and the output order.

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

Build the tool checks.

1

Files found.

import os n = 0 for root, dirs, files in os.walk("."): n += len(files) print(n > 0)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
True
2

First sorted path.

names = ["b/x.txt", "a/x.txt"] print(sorted(names)[0])

Before you run it, say out loud what it will print. Then open the answer.

What it prints
a/x.txt
3

Files kept.

names = ["a.txt", "b.bin"] keep = [x for x in names if x.endswith(".txt")] print(len(keep))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
1
4

Total characters.

n = 0 for line in ["a", "b"]: n += len(line) print(n)

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 tool walks, joins paths, filters, streams each file, and reports every skip with a reason and an exit code.
1 walk the tree
↓
2 join and filter
↓
3 stream each file
↓
4 report skips and exit
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.

import os sets nothing on screen
n = 0 sets nothing on screen
for root, dirs, files in os.walk("."): sets nothing on screen
n += len(files) sets nothing on screen
print(n > 0) True
Why this works
Totals need skips

A tool that counts files must count skips too. A total that ignores them is a wrong answer that looks right.

The exit code is the contract

A person sees the message, a script sees the exit code. Set both, or a caller trusts a broken run.

Examples

Walk and report

import os
n = 0
for root, dirs, files in os.walk("."):
    n += len(files)
print(n > 0)

The walk gives names, not paths. Join each name to its folder before opening.

Order the output

names = ["b/x.txt", "a/x.txt"]
print(sorted(names)[0])

Sort the full paths. Sorting folders then files gives a different tree order.

Skip unreadable ones

names = ["a.txt", "b.bin"]
keep = [x for x in names if x.endswith(".txt")]
print(len(keep))

Filter before opening. One unreadable file should not end the whole run.

Report per file

import sys
print("b.bin: skipped", file=sys.stderr)

Name each skip with a reason. A tool that drops files silently gives a wrong total.

Stream each file

n = 0
for line in ["a", "b"]:
    n += len(line)
print(n)

One file at a time and one line at a time. The tool works on a tree of any size.

Exit code carries it

import sys
sys.exit(0)
print("clean")

A nonzero exit tells a script the run failed. Printing alone leaves a caller reading success.

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.

← Binary and text modes
Previous Lesson
Binary and text modes