get_file_pathnames¶
- pyhelpers.dirs.get_file_pathnames(path_to_dir, file_ext=None, incl_subdir=False)[source]¶
Get paths of files in a directory matching the specified file extension.
This function retrieves paths of files within the directory specified by
path_to_dir
. Optionally, it filters files by the exactfile_ext
specified. Iffile_ext=None
, it returns paths for all files. Ifincl_subdir=True
, it traverses subdirectories recursively.- Parameters:
path_to_dir (str | os.PathLike) – Path to the directory.
file_ext (str | None) – Exact file extension to filter files; defaults to None.
incl_subdir (bool) – Whether to include files from subdirectories; when
incl_subdir=True
, it includes files from all subdirectories recursively; defaults to False.
- Returns:
List of file paths matching the criteria.
- Return type:
list
Examples:
>>> from pyhelpers.dirs import get_file_pathnames >>> test_dir_name = "tests/data" >>> # Get all files in the folder (without sub-folders) >>> get_file_pathnames(test_dir_name) ['tests/data/csr_mat.npz', 'tests/data/dat.csv', 'tests/data/dat.feather', 'tests/data/dat.joblib', 'tests/data/dat.json', 'tests/data/dat.pickle', 'tests/data/dat.txt', 'tests/data/dat.xlsx', 'tests/data/zipped', 'tests/data/zipped.7z', 'tests/data/zipped.txt', 'tests/data/zipped.zip'] >>> get_file_pathnames(test_dir_name, file_ext=".txt") ['tests/data/dat.txt', 'tests/data/zipped.txt'] >>> # Get absolute pathnames of all files contained in the folder (incl. all subdirectories) >>> get_file_pathnames(test_dir_name, file_ext="txt", incl_subdir=True) ['tests/data/dat.txt', 'tests/data/zipped.txt', 'tests/data/zipped/zipped.txt']