Skip to content

Instantly share code, notes, and snippets.

@adriaanbd
Created July 18, 2020 05:38
Show Gist options
  • Save adriaanbd/c203d182d22726b28a00c6066f7267fc to your computer and use it in GitHub Desktop.
Save adriaanbd/c203d182d22726b28a00c6066f7267fc to your computer and use it in GitHub Desktop.
Gets a list of of paths of all pdf files under target directory
from pathlib import Path
def get_pdfs(path: str) -> list:
"""Returns a list of paths of all pdf files under a target directory."""
assert isinstance(path, str), 'Path must be a string'
path_obj = Path(path)
assert path_obj.is_dir(), 'Path must be an existing directory'
path_iter = path_obj.rglob('*.pdf')
file_list = [str(f) for f in path_iter if f.is_file()]
return file_list
@adriaanbd
Copy link
Author

adriaanbd commented Jul 18, 2020

Use like:

get_pdfs('target_directory')
get_pdfs('./target_dir')
get_pdfs('')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment