Download utils/file_system_reader.py from BhojGC/MyApp: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/BhojGC/MyApp/resolve/main/utils/file_system_reader.py
- Command line
-
hf download hf://spaces/BhojGC/MyApp/utils/file_system_reader.py
-
curl -L -o file_system_reader.py https://huggingface.co/spaces/BhojGC/MyApp/resolve/main/utils/file_system_reader.py
1.06 kB
| import os | |
| import json | |
| class FileSystemReader: | |
| BASE_DIR = os.path.dirname(os.path.dirname(__file__)) | |
| DATA_DIR = os.path.join(BASE_DIR, "data") | |
| def __init__(self, file_name): | |
| self.file_name = file_name | |
| self.file_path = self.build_path() | |
| def build_path(self): | |
| # ✅ FIX: if absolute path is given, use it directly | |
| if os.path.isabs(self.file_name): | |
| return self.file_name | |
| return os.path.join(self.DATA_DIR, self.file_name) | |
| def read_file(self): | |
| try: | |
| with open(self.file_path, "r", encoding="utf-8") as file: | |
| return json.load(file) | |
| except FileNotFoundError: | |
| return { | |
| "success": False, | |
| "message": f"File not found: {self.file_path}", | |
| "data": None | |
| } | |
| except json.JSONDecodeError as e: | |
| return { | |
| "success": False, | |
| "message": "Invalid JSON format", | |
| "error": str(e), | |
| "data": None | |
| } |