Safely read content from a file. Args: path: File path encoding: File encoding Returns: File content Raises: FileSystemError: If read fails
(path: Path, encoding: str = "utf-8")
| 87 | |
| 88 | def safe_read(path: Path, encoding: str = "utf-8") -> str: |
| 89 | """ |
| 90 | Safely read content from a file. |
| 91 | |
| 92 | Args: |
| 93 | path: File path |
| 94 | encoding: File encoding |
| 95 | |
| 96 | Returns: |
| 97 | File content |
| 98 | |
| 99 | Raises: |
| 100 | FileSystemError: If read fails |
| 101 | """ |
| 102 | path = Path(path).expanduser().resolve() |
| 103 | |
| 104 | try: |
| 105 | with open(path, "r", encoding=encoding) as f: |
| 106 | return f.read() |
| 107 | except FileNotFoundError: |
| 108 | raise FileSystemError(f"File not found: {path}") |
| 109 | except PermissionError: |
| 110 | raise FileSystemError(f"Permission denied: Cannot read {path}") |
| 111 | except Exception as e: |
| 112 | raise FileSystemError(f"Cannot read {path}: {e}") |
| 113 | |
| 114 | |
| 115 | def get_file_size(path: Path) -> int: |
| 116 | """ |
no test coverage detected