Normalize path, eliminating double slashes, etc.
(path)
| 494 | |
| 495 | except ImportError: |
| 496 | def normpath(path): |
| 497 | """Normalize path, eliminating double slashes, etc.""" |
| 498 | path = os.fspath(path) |
| 499 | if isinstance(path, bytes): |
| 500 | sep = b'\\' |
| 501 | altsep = b'/' |
| 502 | curdir = b'.' |
| 503 | pardir = b'..' |
| 504 | else: |
| 505 | sep = '\\' |
| 506 | altsep = '/' |
| 507 | curdir = '.' |
| 508 | pardir = '..' |
| 509 | path = path.replace(altsep, sep) |
| 510 | prefix, path = splitdrive(path) |
| 511 | |
| 512 | # collapse initial backslashes |
| 513 | if path.startswith(sep): |
| 514 | prefix += sep |
| 515 | path = path.lstrip(sep) |
| 516 | |
| 517 | comps = path.split(sep) |
| 518 | i = 0 |
| 519 | while i < len(comps): |
| 520 | if not comps[i] or comps[i] == curdir: |
| 521 | del comps[i] |
| 522 | elif comps[i] == pardir: |
| 523 | if i > 0 and comps[i-1] != pardir: |
| 524 | del comps[i-1:i+1] |
| 525 | i -= 1 |
| 526 | elif i == 0 and prefix.endswith(sep): |
| 527 | del comps[i] |
| 528 | else: |
| 529 | i += 1 |
| 530 | else: |
| 531 | i += 1 |
| 532 | # If the path is now empty, substitute '.' |
| 533 | if not prefix and not comps: |
| 534 | comps.append(curdir) |
| 535 | return prefix + sep.join(comps) |
| 536 | |
| 537 | else: |
| 538 | def normpath(path): |
no test coverage detected