get_file_pathnames¶
- pyhelpers.dirs.get_file_pathnames(path_to_dir, file_ext=None, incl_subdir=False, abs_path=False, normalized=True, add_slash=False)[source]¶
Gets 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.abs_path (bool) – Whether to return absolute pathname(s).
normalized (bool) – Whether to normalize the returned pathname; defaults to
True
.add_slash (bool) – If
True
, adds a leading slash (and, if appropriate, a trailing slash) to the returned pathname; defaults toFalse
.
- Returns:
List of file paths matching the criteria.
- Return type:
list
Examples:
>>> from pyhelpers.dirs import get_file_pathnames, delete_dir >>> from pyhelpers.store import unzip >>> import os >>> test_dir_name = "tests/data" >>> # Get all files in the directory (without subdirectories) on Windows >>> get_file_pathnames(test_dir_name, add_slash=True) ['./tests/data/csr_mat.npz', './tests/data/dat.csv', './tests/data/dat.feather', './tests/data/dat.joblib', './tests/data/dat.json', './tests/data/dat.ods', './tests/data/dat.pickle', './tests/data/dat.pickle.bz2', './tests/data/dat.pickle.gz', './tests/data/dat.pickle.xz', './tests/data/dat.txt', './tests/data/dat.xlsx', './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'] >>> output_dir = unzip('tests/data/zipped.zip', ret_output_dir=True) >>> os.listdir(output_dir) ['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, abs_path=True) ['<Parent directories>/tests/data/dat.txt', '<Parent directories>/tests/data/zipped.txt', '<Parent directories>/tests/data/zipped/zipped.txt'] >>> delete_dir(output_dir, confirmation_required=False)