Walk Directory Trees Safely with Python pathlib Path.walk
Python 3.12 added pathlib.Path.walk(), bringing directory-tree traversal directly to Path objects. It fills the same broad role as os.walk(), but keeping traversal and path manipulation in pathlib can make filesystem code easier to read. Walking a tree is deceptively simple, though. Production code needs to decide which subtrees to enter, what to do with unreadable directories, whether symbolic links should be followed, and whether the filesystem may change during traversal.