List All Files in a Directory Recursively with Full Paths [python]
The Code
The Output
Notes
-
This uses Path's .walk instead of os.walk so that Path objects are returned instead of strings. This requires python 3.12 or later.
-
Full paths are returned.
-
You can get relative paths by removing the
.resolve()method from thePath(dir)call. -
This skips hidden direcotires that start with a dot and temporary files that end with a
~. -
The
dirnames[:] = [d for d in dirnames if not d.startswith('.')]line updates the directory name in place so thatos.walkskips the hidden directories entirely instead of decending into them. -
The results are sorted before returning them from the method.