@@ -213,3 +213,45 @@ def exists_and_is_accessible(path: Path) -> bool:
213
213
return False
214
214
else :
215
215
raise
216
+
217
+
218
+ def is_in_path (path : Union [str , Path ], parent_path : Union [str , Path ]) -> bool :
219
+ """
220
+ Check if a path is inside another path.
221
+
222
+ Args:
223
+ path: The path to check.
224
+ parent_path: The potential parent path.
225
+
226
+ Returns:
227
+ Whether the path is inside the parent path.
228
+ """
229
+ if not isinstance (path , Path ):
230
+ path = Path (str (path ))
231
+ if not isinstance (parent_path , Path ):
232
+ parent_path = Path (str (parent_path ))
233
+
234
+ # Resolve both paths to absolute paths
235
+ path = path .absolute ()
236
+ parent_path = parent_path .absolute ()
237
+
238
+ # Check if path is a subpath of parent_path
239
+ try :
240
+ # In Python 3.9+, we could use is_relative_to
241
+ # return path.is_relative_to(parent_path)
242
+
243
+ # For compatibility with Python 3.8 and earlier
244
+ path_str = str (path )
245
+ parent_path_str = str (parent_path )
246
+
247
+ # Check if paths are the same
248
+ if path_str == parent_path_str :
249
+ return True
250
+
251
+ # Ensure parent_path ends with a separator to avoid partial matches
252
+ if not parent_path_str .endswith (os .sep ):
253
+ parent_path_str += os .sep
254
+
255
+ return path_str .startswith (parent_path_str )
256
+ except (ValueError , OSError ):
257
+ return False
0 commit comments