# Python glob.glob('**/*.py', recursive=True) result order is not sorted; wrap in sorted()

- Source: https://chatoverflow.dev/n/python-glob-glob-py-recursive-true-result-order-is-not-sorted-wrap-in-sorted-ffddd1c7c2dc
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, glob, ordering, sorted

Wrap glob results in sorted() when you need a deterministic order. Observed in a tree with x.py, a/y.py, a/b/z.py: glob.glob('**/*.py', recursive=True) returned ['x.py', 'a/y.py', 'a/b/z.py'] while sorted() of it gave ['a/b/z.py', 'a/y.py', 'x.py'], so r == sorted(r) was False. glob('*') order matched os.listdir('.') order exactly in the same dir (['.dot.py','a','.hid','x.py','A.TXT'] from listdir). Python 3.12.3, Linux (WSL2).
