Scan a directory to find the interested files. Args: dir_path (str): Path of the directory. suffix (str | tuple(str), optional): File suffix that we are interested in. Default: None. recursive (bool, optional): If set to True, recursively scan the
(dir_path, suffix=None, recursive=False, full_path=False)
| 50 | |
| 51 | |
| 52 | def scandir(dir_path, suffix=None, recursive=False, full_path=False): |
| 53 | """Scan a directory to find the interested files. |
| 54 | |
| 55 | Args: |
| 56 | dir_path (str): Path of the directory. |
| 57 | suffix (str | tuple(str), optional): File suffix that we are |
| 58 | interested in. Default: None. |
| 59 | recursive (bool, optional): If set to True, recursively scan the |
| 60 | directory. Default: False. |
| 61 | full_path (bool, optional): If set to True, include the dir_path. |
| 62 | Default: False. |
| 63 | |
| 64 | Returns: |
| 65 | A generator for all the interested files with relative paths. |
| 66 | """ |
| 67 | |
| 68 | if (suffix is not None) and not isinstance(suffix, (str, tuple)): |
| 69 | raise TypeError('"suffix" must be a string or tuple of strings') |
| 70 | |
| 71 | root = dir_path |
| 72 | |
| 73 | def _scandir(dir_path, suffix, recursive): |
| 74 | for entry in os.scandir(dir_path): |
| 75 | if not entry.name.startswith('.') and entry.is_file(): |
| 76 | if full_path: |
| 77 | return_path = entry.path |
| 78 | else: |
| 79 | return_path = osp.relpath(entry.path, root) |
| 80 | |
| 81 | if suffix is None: |
| 82 | yield return_path |
| 83 | elif return_path.endswith(suffix): |
| 84 | yield return_path |
| 85 | else: |
| 86 | if recursive: |
| 87 | yield from _scandir(entry.path, suffix=suffix, recursive=recursive) |
| 88 | else: |
| 89 | continue |
| 90 | |
| 91 | return _scandir(dir_path, suffix=suffix, recursive=recursive) |
| 92 | |
| 93 | |
| 94 | def check_resume(opt, resume_iter): |
no test coverage detected