check_files_exist

pyhelpers.dirs.check_files_exist(filenames, dir_path, verbose=False, **kwargs)[source]

Check whether specified files exist within a given directory.

This function compares files by basename only, not by their full relative path – if filenames includes a path with subdirectory components, only the final filename portion is compared, so a same-named file located in a different subdirectory of dir_path (e.g. when incl_subdir=True is passed via kwargs) would count as a match.

Parameters:
  • filenames (Iterable) – Filenames to check for existence.

  • dir_path (str | os.PathLike) – Pathname of the directory in which to check for the files.

  • verbose (bool | int) – Whether to print relevant information to the console. Defaults to False.

  • kwargs – [Optional] Additional parameters for get_file_paths() (e.g. incl_subdir=True to also check files within subdirectories).

Returns:

True if all queried files exist in the directory, False otherwise.

Return type:

bool

Examples:

>>> from pyhelpers.dirs import check_files_exist

>>> test_dir_name = "tests/data"

>>> # Check if all required files exist in the directory
>>> check_files_exist(["dat.csv", "dat.txt"], dir_path=test_dir_name)
True

>>> # If not all required files exist, print the missing files
>>> check_files_exist(("dat.csv", "dat.txt", "dat_0.txt"), test_dir_name, verbose=True)
Error: Required files are not satisfied, missing files are: ['dat_0.txt']
False