get_file_in_dir(directory: str | CloudPath | Path, pattern_str: str, extension: str | None = None, filename_only: bool = False, get_list: bool = False, exact_name: bool = False) CloudPath | Path | list[source]#

Deprecated since version 1.30.0: Import it from sertit.path instead of sertit.files

Get one or all matching files (pattern + extension) from inside a directory.

Note that the pattern is a regex with glob’s convention, i.e. *pattern*.

If exact_name is False, the searched pattern will be *{pattern}*.{extension}, else {pattern}.{extension}.

  • directory (str) – Directory where to find the files

  • pattern_str (str) – Pattern wanted as a string, with glob’s convention.

  • extension (str) – Extension wanted, optional. With or without point. (yaml or .yaml accepted)

  • filename_only (bool) – Get only the filename

  • get_list (bool) – Get the whole list of matching files

  • exact_name (bool) – Get the exact name (without adding * before and after the given pattern)



Return type:

Union[AnyPathType, list]


>>> directory = 'D:/path/to/dir'
>>> os.listdir(directory)
["haha.txt", "huhu1.txt", "huhu1.geojson", "hoho.txt"]
>>> get_file_in_dir(directory, "huhu")
>>> get_file_in_dir(directory, "huhu", extension="txt")
>>> get_file_in_dir(directory, "huhu", get_list=True)
['D:/path/to/dir/huhu1.txt', 'D:/path/to/dir/huhu1.geojson']
>>> get_file_in_dir(directory, "huhu", filename_only=True, get_list=True)
['huhu1.txt', 'huhu1.geojson']
>>> get_file_in_dir(directory, "huhu", get_list=True, exact_name=True)