Examples > nested-catalog COMPANION MAKE_NESTED_CATALOG.PY (3/4) def write_index(folder: pathlib.Path, root: pathlib.Path) -> int: """Writes folder/index.xml (recursively) and returns the number of books below folder.""" rows, budget = [], 0 subfolders = sorted(p for p in folder.iterdir() if p.is_dir() and not p.name.startswith(".")) total = 0 for sub in subfolders: n = write_index(sub, root) if n == 0: continue # no empty folders on the reader total += n rows.append(entry(sub.name, f"{n} book" + ("" if n == 1 else "s"), f"{sub.name}/{INDEX}", is_folder=True)) for book in sorted(p for p in folder.iterdir() if is_book(p)): title, author = epub_meta(book) if book.suffix.lower() == ".epub" else (None, None) rows.append(entry(title or book.stem, author, book.name, file_name=book.name)) total += 1 if total == 0: stale = folder / INDEX if stale.exists(): stale.unlink() return 0 Next: 07 Companion make_nested_catalog.py (4/4)