Created
October 29, 2022 16:17
-
-
Save dcbark01/ed5268a571911829d49122b53af3cb37 to your computer and use it in GitHub Desktop.
New listdirs
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
import os | |
from typing import List, Union | |
def listdirs(path, extensions: Union[List[str], str] = None): | |
""" List all files in directory (including walking all subdirectories). | |
Can filter by file extension by providing either, for example: | |
extensions='png' | |
extensions=['png', 'jpeg'] | |
""" | |
output = [] | |
for root, dirs, files in os.walk(path): | |
for file in files: | |
output.append(os.path.join(root, file)) | |
if extensions: | |
if isinstance(extensions, str): | |
extensions = [extensions] | |
def filter_ext(f): | |
return True if os.path.splitext(f)[-1].strip('.') in extensions else False | |
output = list(filter(filter_ext, output)) | |
return output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment