Created
February 27, 2022 13:02
-
-
Save Enchan1207/2c32891974b6f4014340c0eaf236a579 to your computer and use it in GitHub Desktop.
ディレクトリの再帰検索
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 listdir_r(path: str) -> List[str]: | |
"""ディレクトリの再帰検索 | |
Args: | |
path (str): 検索対象 | |
Returns: | |
List[str]: 検索結果 | |
""" | |
def _listdir(path: str) -> List[str]: | |
""" 拡張listdir """ | |
try: | |
return [f"{path}/{name}" for name in os.listdir(path)] | |
except NotADirectoryError: | |
return [] | |
contents = _listdir(path) | |
files = list(filter(lambda path: os.path.isfile(path), contents)) | |
dirs = list(filter(lambda path: os.path.isdir(path), contents)) | |
for dir in dirs: | |
files += listdir_r(dir) | |
return files |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment