MCPcopy Create free account
hub / github.com/FSoft-AI4Code/CodeWiki / safe_read

Function safe_read

codewiki/cli/utils/fs.py:89–113  ·  view source on GitHub ↗

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")

Source from the content-addressed store, hash-verified

87
88def 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
115def get_file_size(path: Path) -> int:
116 """

Callers 5

load_module_treeMethod · 0.90
load_metadataMethod · 0.90
generateMethod · 0.90
loadMethod · 0.90

Calls 2

FileSystemErrorClass · 0.90
resolveMethod · 0.80

Tested by

no test coverage detected