Normalize path, eliminating double slashes, etc.
(path)
| 343 | |
| 344 | except ImportError: |
| 345 | def normpath(path): |
| 346 | """Normalize path, eliminating double slashes, etc.""" |
| 347 | path = os.fspath(path) |
| 348 | if isinstance(path, bytes): |
| 349 | sep = b'/' |
| 350 | dot = b'.' |
| 351 | dotdot = b'..' |
| 352 | else: |
| 353 | sep = '/' |
| 354 | dot = '.' |
| 355 | dotdot = '..' |
| 356 | if not path: |
| 357 | return dot |
| 358 | _, initial_slashes, path = splitroot(path) |
| 359 | comps = path.split(sep) |
| 360 | new_comps = [] |
| 361 | for comp in comps: |
| 362 | if not comp or comp == dot: |
| 363 | continue |
| 364 | if (comp != dotdot or (not initial_slashes and not new_comps) or |
| 365 | (new_comps and new_comps[-1] == dotdot)): |
| 366 | new_comps.append(comp) |
| 367 | elif new_comps: |
| 368 | new_comps.pop() |
| 369 | comps = new_comps |
| 370 | path = initial_slashes + sep.join(comps) |
| 371 | return path or dot |
| 372 | |
| 373 | |
| 374 | def abspath(path): |