MCPcopy
hub / github.com/keon/algorithms / simplify_path

Function simplify_path

algorithms/stack/simplify_path.py:17–41  ·  view source on GitHub ↗

Simplify a Unix-style absolute path. Args: path: An absolute file path string. Returns: The simplified canonical path. Examples: >>> simplify_path("/home/") '/home' >>> simplify_path("/a/./b/../../c/") '/c'

(path: str)

Source from the content-addressed store, hash-verified

15
16
17def simplify_path(path: str) -> str:
18 """Simplify a Unix-style absolute path.
19
20 Args:
21 path: An absolute file path string.
22
23 Returns:
24 The simplified canonical path.
25
26 Examples:
27 >>> simplify_path("/home/")
28 '/home'
29 >>> simplify_path("/a/./b/../../c/")
30 '/c'
31 """
32 skip = {"..", ".", ""}
33 stack: list[str] = []
34 tokens = path.split("/")
35 for token in tokens:
36 if token == "..":
37 if stack:
38 stack.pop()
39 elif token not in skip:
40 stack.append(token)
41 return "/" + "/".join(stack)

Callers 1

test_simplify_pathMethod · 0.90

Calls 1

popMethod · 0.45

Tested by 1

test_simplify_pathMethod · 0.72