Download apps/automation/utils/file_utils.py from jun2024/fc: direct link, hf CLI and curl.
- Browser
- Download file 1.89 kB
-
https://huggingface.co/spaces/jun2024/fc/resolve/main/apps/automation/utils/file_utils.py
- Command line
-
hf download hf://spaces/jun2024/fc/apps/automation/utils/file_utils.py
-
curl -L -o file_utils.py https://huggingface.co/spaces/jun2024/fc/resolve/main/apps/automation/utils/file_utils.py
1.89 kB
| """ | |
| 文件处理工具类 | |
| 纯工具方法,不包含业务逻辑 | |
| """ | |
| import logging | |
| from pathlib import Path | |
| from typing import Any | |
| logger = logging.getLogger("apps.automation") | |
| class FileUtils: | |
| """文件处理相关工具方法""" | |
| def validate_file_basic(file_path: str, expected_extensions: list[Any] | None = None) -> dict[str, Any]: | |
| """ | |
| 基础文件校验 | |
| Args: | |
| file_path: 文件路径 | |
| expected_extensions: 期望的文件扩展名列表 | |
| Returns: | |
| 校验结果 {valid: bool, error: str, info: dict} | |
| """ | |
| result: dict[str, Any] = {"valid": True, "error": None, "info": {}} | |
| # 检查文件是否存在 | |
| if not Path(file_path).exists(): | |
| result["valid"] = False | |
| result["error"] = "文件不存在" | |
| return result | |
| # 检查文件大小 | |
| file_size = Path(file_path).stat().st_size | |
| result["info"]["size"] = file_size | |
| if file_size == 0: | |
| result["valid"] = False | |
| result["error"] = "文件为空" | |
| return result | |
| # 检查文件扩展名 | |
| ext = Path(file_path).suffix.lower() | |
| result["info"]["extension"] = ext | |
| if expected_extensions and ext not in expected_extensions: | |
| result["valid"] = False | |
| result["error"] = f"文件类型不匹配,期望: {expected_extensions}" | |
| return result | |
| # 检查文件是否损坏(简单检查) | |
| try: | |
| with open(file_path, "rb") as f: | |
| f.read(1024) # 尝试读取前1KB | |
| except Exception as e: | |
| result["valid"] = False | |
| result["error"] = f"文件可能损坏: {e!s}" | |
| return result | |
| logger.info(f"文件校验通过: {file_path} ({file_size} bytes)") | |
| return result | |