Normalize path, eliminating double slashes, etc.
(path)
| 348 | |
| 349 | except ImportError: |
| 350 | def normpath(path): |
| 351 | """Normalize path, eliminating double slashes, etc.""" |
| 352 | path = os.fspath(path) |
| 353 | if isinstance(path, bytes): |
| 354 | sep = b'/' |
| 355 | empty = b'' |
| 356 | dot = b'.' |
| 357 | dotdot = b'..' |
| 358 | else: |
| 359 | sep = '/' |
| 360 | empty = '' |
| 361 | dot = '.' |
| 362 | dotdot = '..' |
| 363 | if path == empty: |
| 364 | return dot |
| 365 | initial_slashes = path.startswith(sep) |
| 366 | # POSIX allows one or two initial slashes, but treats three or more |
| 367 | # as single slash. |
| 368 | # (see https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13) |
| 369 | if (initial_slashes and |
| 370 | path.startswith(sep*2) and not path.startswith(sep*3)): |
| 371 | initial_slashes = 2 |
| 372 | comps = path.split(sep) |
| 373 | new_comps = [] |
| 374 | for comp in comps: |
| 375 | if comp in (empty, dot): |
| 376 | continue |
| 377 | if (comp != dotdot or (not initial_slashes and not new_comps) or |
| 378 | (new_comps and new_comps[-1] == dotdot)): |
| 379 | new_comps.append(comp) |
| 380 | elif new_comps: |
| 381 | new_comps.pop() |
| 382 | comps = new_comps |
| 383 | path = sep.join(comps) |
| 384 | if initial_slashes: |
| 385 | path = sep*initial_slashes + path |
| 386 | return path or dot |
| 387 | |
| 388 | else: |
| 389 | def normpath(path): |