python 39 lines · 8 steps

Polling a directory for file changes in Python

A simple poller that detects created, deleted, and modified files by diffing snapshots of modification times.

Explained by highlit
1import os
2import time
3from pathlib import Path
4 
5 
6def watch_directory(path, on_change, interval=1.0, pattern="*"):
7 directory = Path(path)
8 snapshot = _snapshot(directory, pattern)
9 
10 while True:
11 time.sleep(interval)
12 current = _snapshot(directory, pattern)
13 
14 created = current.keys() - snapshot.keys()
15 deleted = snapshot.keys() - current.keys()
16 modified = {
17 f for f in current.keys() & snapshot.keys()
18 if current[f] != snapshot[f]
19 }
20 
21 for f in sorted(created):
22 on_change("created", f)
23 for f in sorted(deleted):
24 on_change("deleted", f)
25 for f in sorted(modified):
26 on_change("modified", f)
27 
28 snapshot = current
29 
30 
31def _snapshot(directory, pattern):
32 snapshot = {}
33 for entry in directory.glob(pattern):
34 if entry.is_file():
35 try:
36 snapshot[str(entry)] = entry.stat().st_mtime_ns
37 except OSError:
38 continue
39 return snapshot
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Comparing two snapshots with set operations cleanly separates created, deleted, and modified into three cases.
  2. 2Storing modification times lets you detect edits, not just additions and removals.
  3. 3Polling trades responsiveness and CPU cost against simplicity — no OS-specific file-watch APIs needed.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Polling a directory for file changes in Python — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code