[+] Create helper functions for reading and writing files
This commit is contained in:
@@ -4,6 +4,7 @@ import json
|
|||||||
import os
|
import os
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime, date
|
from datetime import datetime, date
|
||||||
|
from pathlib import Path
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
import json5
|
import json5
|
||||||
@@ -90,6 +91,35 @@ def calculate_rate_delay(rate_limit: float) -> float:
|
|||||||
return 1 / rate_limit * 60
|
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):
|
class EnhancedJSONEncoder(json.JSONEncoder):
|
||||||
def default(self, o):
|
def default(self, o):
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user