find_files

Contents

find_files#

find_files(names: list | str, root_paths: list | str | CloudPath | Path, max_nof_files: int = -1, get_as_str: bool = False) list | str[source]#

Returns matching files recursively from a list of root paths.

Regex are allowed (using glob)

Parameters:
  • names (Union[list, str]) – File names.

  • root_paths (Union[list, str]) – Root paths

  • max_nof_files (int) – Maximum number of files (set to -1 for unlimited)

  • get_as_str (bool) – if only one file is found, it can be retrieved as a string instead of a list

Returns:

File name

Return type:

list

Example

>>> root_path = 'D:/root'
>>> dir1_path = 'D:/root/dir1'
>>> dir2_path = 'D:/root/dir2'
>>>
>>> os.listdir(dir1_path)
["haha.txt", "huhu.txt", "hoho.txt"]
>>> os.listdir(dir2_path)
["huhu.txt", "hehe.txt"]
>>>
>>> find_files("huhu.txt", root_path)
['D:/root/dir1/huhu.txt', 'D:/root/dir2/huhu.txt']
>>>
>>> find_files("huhu.txt", root_path, max_nof_files=1)
['D:/root/dir1/huhu.txt']
>>>
>>> find_files("huhu.txt", root_path, max_nof_files=1, get_as_str=True)
found = 'D:/root/dir1/huhu.txt'