get_file_pathnames

pyhelpers.dirs.get_file_pathnames(path_to_dir, file_ext=None, incl_subdir=False)[source]

Get all files in the folder with the specified file type.

Parameters:
  • path_to_dir (str | os.PathLike) – a folder path

  • file_ext (str | None) – exact file type to specify, if file_type is "*" or "all", return all files in the folder; defaults to None

  • incl_subdir (bool) – whether to get files inside the subfolder, defaults to False; when incl_subdir=True, the function traverses all subfolders

Returns:

a list of file paths

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 sub-folders)
>>> 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']