File size: 627 Bytes
80ebcb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import pathlib
from typing import List


def find_files(root: str, pattern: str, depth: int = 0) -> List[str]:
    root_path = pathlib.Path(root)
    result_files = []

    def within_depth(path: pathlib.Path) -> bool:
        return len(path.relative_to(root_path).parts) <= depth

    if depth == 0:
        result_files.extend([str(file) for file in root_path.glob(pattern)])
    else:
        # rglob matches all levels, but we filter by depth
        for file in root_path.rglob(pattern):
            if file.is_file() and within_depth(file.parent):
                result_files.append(str(file))

    return result_files