Last active
October 25, 2023 21:50
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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