# 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