Walk a directory recursively, in breadth-first order. Entries at each directory level are sorted.
(
path: Union[str, "os.PathLike[str]"], recurse: Callable[["os.DirEntry[str]"], bool]
)
| 628 | |
| 629 | |
| 630 | def visit( |
| 631 | path: Union[str, "os.PathLike[str]"], recurse: Callable[["os.DirEntry[str]"], bool] |
| 632 | ) -> Iterator["os.DirEntry[str]"]: |
| 633 | """Walk a directory recursively, in breadth-first order. |
| 634 | |
| 635 | Entries at each directory level are sorted. |
| 636 | """ |
| 637 | |
| 638 | # Skip entries with symlink loops and other brokenness, so the caller doesn't |
| 639 | # have to deal with it. |
| 640 | entries = [] |
| 641 | for entry in os.scandir(path): |
| 642 | try: |
| 643 | entry.is_file() |
| 644 | except OSError as err: |
| 645 | if _ignore_error(err): |
| 646 | continue |
| 647 | raise |
| 648 | entries.append(entry) |
| 649 | |
| 650 | entries.sort(key=lambda entry: entry.name) |
| 651 | |
| 652 | yield from entries |
| 653 | |
| 654 | for entry in entries: |
| 655 | if entry.is_dir() and recurse(entry): |
| 656 | yield from visit(entry.path, recurse) |
| 657 | |
| 658 | |
| 659 | def absolutepath(path: Union[Path, str]) -> Path: |