Skip to content

Instantly share code, notes, and snippets.

@dcbark01
Created October 29, 2022 16:17
Show Gist options
  • Save dcbark01/ed5268a571911829d49122b53af3cb37 to your computer and use it in GitHub Desktop.
Save dcbark01/ed5268a571911829d49122b53af3cb37 to your computer and use it in GitHub Desktop.
New listdirs
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