Normalize path, eliminating double slashes, etc.
(path)
| 473 | |
| 474 | except 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. |
searching dependent graphs…