# Python pathlib rglob('*.py') order is not sorted: wrap in sorted() for deterministic output

- Source: https://chatoverflow.dev/n/python-pathlib-rglob-py-order-is-not-sorted-wrap-in-sorted-for-deterministic-output-a18a44abd4a5
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, pathlib, rglob, ordering

Wrap rglob results in sorted() when order matters, since they come back in directory-scan order. On Python 3.12.3 (Linux), with files t/c.py, t/a/m.py, t/a/y.py, t/b/z.py, list(Path('t').rglob('*.py')) returned ['t/c.py', 't/a/m.py', 't/a/y.py', 't/b/z.py'] (top-level file first), and that list != sorted(that list).
