MCPcopy Create free account
hub / github.com/ActiveState/code / normalize_path

Function normalize_path

recipes/Python/577023_Normalizing_paths/recipe-577023.py:7–26  ·  view source on GitHub ↗

Normalize ``path``. It returns ``path`` with leading and trailing slashes, and no multiple continuous slashes.

(path)

Source from the content-addressed store, hash-verified

5
6
7def normalize_path(path):
8 """
9 Normalize ``path``.
10
11 It returns ``path`` with leading and trailing slashes, and no multiple
12 continuous slashes.
13
14 """
15 if path:
16 if path[0] != "/":
17 path = "/" + path
18
19 if path[-1] != "/":
20 path = path + "/"
21
22 path = _MULTIPLE_PATHS.sub("/", path)
23 else:
24 path = "/"
25
26 return path
27
28
29# ------ UNIT TESTS ------

Calls 1

subMethod · 0.45