Directory tree generator. This behaves exactly like walk(), except that it yields a 4-tuple dirpath, dirnames, filenames, dirfd `dirpath`, `dirnames` and `filenames` are identical to walk() output, and `dirfd` is a file descriptor referring to the directory `di
(top=".", topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None)
| 437 | if {open, stat} <= supports_dir_fd and {scandir, stat} <= supports_fd: |
| 438 | |
| 439 | def fwalk(top=".", topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None): |
| 440 | """Directory tree generator. |
| 441 | |
| 442 | This behaves exactly like walk(), except that it yields a 4-tuple |
| 443 | |
| 444 | dirpath, dirnames, filenames, dirfd |
| 445 | |
| 446 | `dirpath`, `dirnames` and `filenames` are identical to walk() output, |
| 447 | and `dirfd` is a file descriptor referring to the directory `dirpath`. |
| 448 | |
| 449 | The advantage of fwalk() over walk() is that it's safe against symlink |
| 450 | races (when follow_symlinks is False). |
| 451 | |
| 452 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 453 | and top should be relative; top will then be relative to that directory. |
| 454 | (dir_fd is always supported for fwalk.) |
| 455 | |
| 456 | Caution: |
| 457 | Since fwalk() yields file descriptors, those are only valid until the |
| 458 | next iteration step, so you should dup() them if you want to keep them |
| 459 | for a longer period. |
| 460 | |
| 461 | Example: |
| 462 | |
| 463 | import os |
| 464 | for root, dirs, files, rootfd in os.fwalk('python/Lib/xml'): |
| 465 | print(root, "consumes", end="") |
| 466 | print(sum(os.stat(name, dir_fd=rootfd).st_size for name in files), |
| 467 | end="") |
| 468 | print("bytes in", len(files), "non-directory files") |
| 469 | if '__pycache__' in dirs: |
| 470 | dirs.remove('__pycache__') # don't visit __pycache__ directories |
| 471 | """ |
| 472 | sys.audit("os.fwalk", top, topdown, onerror, follow_symlinks, dir_fd) |
| 473 | top = fspath(top) |
| 474 | stack = [(_fwalk_walk, (True, dir_fd, top, top, None))] |
| 475 | isbytes = isinstance(top, bytes) |
| 476 | try: |
| 477 | while stack: |
| 478 | yield from _fwalk(stack, isbytes, topdown, onerror, follow_symlinks) |
| 479 | finally: |
| 480 | # Close any file descriptors still on the stack. |
| 481 | while stack: |
| 482 | action, value = stack.pop() |
| 483 | if action == _fwalk_close: |
| 484 | close(value) |
| 485 | |
| 486 | # Each item in the _fwalk() stack is a pair (action, args). |
| 487 | _fwalk_walk = 0 # args: (isroot, dirfd, toppath, topname, entry) |
nothing calls this directly
no test coverage detected