Skip to content

Instantly share code, notes, and snippets.

@Enchan1207
Created February 27, 2022 13:02
Show Gist options
  • Save Enchan1207/2c32891974b6f4014340c0eaf236a579 to your computer and use it in GitHub Desktop.
Save Enchan1207/2c32891974b6f4014340c0eaf236a579 to your computer and use it in GitHub Desktop.
ディレクトリの再帰検索
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