Spaces:
Running on Zero
Running on Zero
File size: 711 Bytes
d10de1b | 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 | """Shared utility helpers."""
import json
import os
def load_json(path: str) -> dict | list:
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
def save_json(data: dict | list, path: str) -> None:
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
def file_exists(path: str) -> bool:
return os.path.exists(path)
def list_files(directory: str, extension: str = ".txt") -> list[str]:
if not os.path.isdir(directory):
return []
return [
os.path.join(directory, f)
for f in os.listdir(directory)
if f.endswith(extension)
]
|