Recursive directory tree generator for directories. Args: top: string, a Directory name topdown: bool, Traverse pre order if True, post order if False. onerror: optional handler for errors. Should be a function, it will be called with the error as argument. Rethrowing the error
(top, topdown=True, onerror=None)
| 665 | |
| 666 | @tf_export("io.gfile.walk") |
| 667 | def walk_v2(top, topdown=True, onerror=None): |
| 668 | """Recursive directory tree generator for directories. |
| 669 | |
| 670 | Args: |
| 671 | top: string, a Directory name |
| 672 | topdown: bool, Traverse pre order if True, post order if False. |
| 673 | onerror: optional handler for errors. Should be a function, it will be |
| 674 | called with the error as argument. Rethrowing the error aborts the walk. |
| 675 | Errors that happen while listing directories are ignored. |
| 676 | |
| 677 | Yields: |
| 678 | Each yield is a 3-tuple: the pathname of a directory, followed by lists of |
| 679 | all its subdirectories and leaf files. |
| 680 | (dirname, [subdirname, subdirname, ...], [filename, filename, ...]) |
| 681 | as strings |
| 682 | """ |
| 683 | top = compat.as_str_any(top) |
| 684 | try: |
| 685 | listing = list_directory(top) |
| 686 | except errors.NotFoundError as err: |
| 687 | if onerror: |
| 688 | onerror(err) |
| 689 | else: |
| 690 | return |
| 691 | |
| 692 | files = [] |
| 693 | subdirs = [] |
| 694 | for item in listing: |
| 695 | full_path = os.path.join(top, item) |
| 696 | if is_directory(full_path): |
| 697 | subdirs.append(item) |
| 698 | else: |
| 699 | files.append(item) |
| 700 | |
| 701 | here = (top, subdirs, files) |
| 702 | |
| 703 | if topdown: |
| 704 | yield here |
| 705 | |
| 706 | for subdir in subdirs: |
| 707 | for subitem in walk_v2(os.path.join(top, subdir), topdown, onerror=onerror): |
| 708 | yield subitem |
| 709 | |
| 710 | if not topdown: |
| 711 | yield here |
| 712 | |
| 713 | |
| 714 | @tf_export(v1=["gfile.Stat"]) |
no test coverage detected