Skip to content

Instantly share code, notes, and snippets.

@dmwyatt
Last active October 25, 2023 21:50
Show Gist options
  • Save dmwyatt/7d7d6b1fca254ef96bbb9f2f1295c703 to your computer and use it in GitHub Desktop.
Save dmwyatt/7d7d6b1fca254ef96bbb9f2f1295c703 to your computer and use it in GitHub Desktop.
[breadth-first path walk] Walk a directory tree yielding all files and directories using a breadth-first algo.
def path_walk(path: str) -> Generator[str, None, None]:
"""Walks a directory tree yielding all files and directories.
This uses a breadth-first algorithm.
"""
queue = deque([path])
while queue:
current = queue.popleft()
for entry in os.scandir(current):
entry: os.DirEntry
if entry.is_dir():
queue.append(entry.path)
yield entry.path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment