From 343e432df942a5dcba5568b3da7bad396141ec10 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Mon, 22 Nov 2021 13:55:03 -0500 Subject: [PATCH] [+] Create helper functions for reading and writing files --- src/utils.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/utils.py b/src/utils.py index e458f4a..567ca3d 100644 --- a/src/utils.py +++ b/src/utils.py @@ -4,6 +4,7 @@ import json import os from dataclasses import dataclass from datetime import datetime, date +from pathlib import Path from typing import Union import json5 @@ -90,6 +91,35 @@ def calculate_rate_delay(rate_limit: float) -> float: return 1 / rate_limit * 60 +def write(file: str, text: str) -> None: + """ + Write text to a file + + :param file: File path + :param text: Text + :return: None + """ + file = file.replace('\\', '/') + + if '/' in file: + path = '/'.join(file.split('/')[:-1]) + Path(path).mkdir(parents=True, exist_ok=True) + + with open(file, 'w', encoding='utf-8') as f: + f.write(text) + + +def read(file: str) -> str: + """ + Read file content + + :param file: File path + :return: None + """ + with open(file, 'r', encoding='utf-8') as f: + return f.read() + + class EnhancedJSONEncoder(json.JSONEncoder): def default(self, o):