Automation: managing files
Renaming 300 photos, sorting downloads, archiving reports… these are perfect jobs for Python. The pathlib module handles paths as objects: folder / "name.txt" builds a path, .glob("*.jpg") finds the files that match a pattern.
A file is read with .read_text(), written with .write_text() and renamed with .rename(). Good practice: test on a temporary folder (tempfile.mkdtemp()) before running a script on your real files, because a botched rename can mix everything up.
from pathlib import Path
import tempfile
folder = Path(tempfile.mkdtemp())
for name in ["report.txt", "photo.jpg", "notes.txt"]:
(folder / name).write_text("demo")
for f in sorted(folder.glob("*.txt")):
print(f.name, f.stat().st_size, "bytes")