MCPcopy Index your code
hub / github.com/python/cpython / normpath

Function normpath

Lib/ntpath.py:475–509  ·  view source on GitHub ↗

Normalize path, eliminating double slashes, etc.

(path)

Source from the content-addressed store, hash-verified

473
474except ImportError:
475 def normpath(path):
476 """Normalize path, eliminating double slashes, etc."""
477 path = os.fspath(path)
478 if isinstance(path, bytes):
479 sep = b'\\'
480 altsep = b'/'
481 curdir = b'.'
482 pardir = b'..'
483 else:
484 sep = '\\'
485 altsep = '/'
486 curdir = '.'
487 pardir = '..'
488 path = path.replace(altsep, sep)
489 drive, root, path = splitroot(path)
490 prefix = drive + root
491 comps = path.split(sep)
492 i = 0
493 while i < len(comps):
494 if not comps[i] or comps[i] == curdir:
495 del comps[i]
496 elif comps[i] == pardir:
497 if i > 0 and comps[i-1] != pardir:
498 del comps[i-1:i+1]
499 i -= 1
500 elif i == 0 and root:
501 del comps[i]
502 else:
503 i += 1
504 else:
505 i += 1
506 # If the path is now empty, substitute '.'
507 if not prefix and not comps:
508 comps.append(curdir)
509 return prefix + sep.join(comps)
510
511
512# Return an absolute path.

Callers 4

abspathFunction · 0.90
_readlink_deepFunction · 0.90
realpathFunction · 0.90

Calls 5

splitrootFunction · 0.90
replaceMethod · 0.45
splitMethod · 0.45
appendMethod · 0.45
joinMethod · 0.45

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…