File size: 1,092 Bytes
b2a5882
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
import json
from typing import Any



def json_load(path: str) -> Any:
    """
    Load and parse a JSON file.

    Args:
        path (str): Path to the JSON file.

    Returns:
        Any: The parsed Python object (usually a dict or list) from the JSON file.
    """
    with open(path, 'r') as f:
        return json.load(f)
    


def get_files(path: str, ext: str = None) -> list[str]:
    """
    Get all files in a directory with a specific extension.

    Args:
        path (str): Folder path to search for files.
        ext (str, optional): Extension that you want to filter. Defaults to None.

    Raises:
        ValueError: If file does not exist.

    Returns:
        list[str]: List of file paths that match the given extension.
    """
    if not os.path.isdir(path):
        raise ValueError(f"Path {path} is not a directory.")
    
    files = []
    for root, _, filenames in os.walk(path):
        for filename in filenames:
            if ext is None or filename.endswith(ext):
                files.append(os.path.join(root, filename))
    
    return files