Skip to content

Instantly share code, notes, and snippets.

@adriaanbd
Created December 20, 2019 16:52
Show Gist options
  • Save adriaanbd/9850cd3a7e9a97dc3cbc5ce241c50ba6 to your computer and use it in GitHub Desktop.
Save adriaanbd/9850cd3a7e9a97dc3cbc5ce241c50ba6 to your computer and use it in GitHub Desktop.
Find all files in a directory
from pathlib import Path
def check_types(suffix, path):
assert isinstance(suffix, str), 'Suffix must be a string'
assert isinstance(path, str), 'Path must be a string'
def find_files(suffix: str, path: str, dirs=False) -> list:
check_types(suffix, path)
path_obj = Path(path)
path_iter = path_obj.rglob(f'*{suffix}')
if dirs:
file_list = [str(f) for f in path_iter]
else:
file_list = [str(f) for f in path_iter if f.is_file()]
return file_list
path = './pictures'
suffix = '.png'
print(find_files(suffix, path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment