File size: 1,518 Bytes
238b11f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
# EvalifAI © tutorial

from typing import List, AnyStr
from pathlib import Path
import json
import os


def create_directory(path):
    path = str(path)
    if os.path.exists(path) is False:
        try:
            os.makedirs(path)
        except OSError:
            return False
    return True


def list_rglob_files(source_dir: str, patterns: list) -> List[AnyStr]:
    """Returns a list of files in source_dir matching patterns

    Author: Flavien BERWICK

    Args:
        source_dir (str): 
            Path to the directory in which looking for files
        patterns list(str):
            List of string patterns to match files in source_dir.
            E.g: ["*.txt", "*.csv"]

    Returns:
        list(str): files path found
    """
    all_files = []
    for pattern in patterns:
        files = []
        for filename in Path(source_dir).rglob(pattern):
            path_data = Path(filename)
            basename = path_data.name
            parent_directory = str(path_data.parent).replace(source_dir, "")
            files.append(f"{source_dir}/{parent_directory}{basename}")
        all_files.extend(files)
    return all_files


def load_json_file(json_file_path):
    if os.path.exists(json_file_path) is False:
        print(f"No such file {json_file_path}")
        return False
    with open(json_file_path, "r") as json_file:
        json_content_file = json_file.read()
    json_element = json.loads(json_content_file)
    if json_element:
        return json_element
    return False