MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / relpath

Function relpath

tools/python-3.11.9-amd64/Lib/posixpath.py:486–519  ·  view source on GitHub ↗

Return a relative version of a path

(path, start=None)

Source from the content-addressed store, hash-verified

484supports_unicode_filenames = (sys.platform == 'darwin')
485
486def relpath(path, start=None):
487 """Return a relative version of a path"""
488
489 if not path:
490 raise ValueError("no path specified")
491
492 path = os.fspath(path)
493 if isinstance(path, bytes):
494 curdir = b'.'
495 sep = b'/'
496 pardir = b'..'
497 else:
498 curdir = '.'
499 sep = '/'
500 pardir = '..'
501
502 if start is None:
503 start = curdir
504 else:
505 start = os.fspath(start)
506
507 try:
508 start_list = [x for x in abspath(start).split(sep) if x]
509 path_list = [x for x in abspath(path).split(sep) if x]
510 # Work out how much of the filepath is shared by start and path.
511 i = len(commonprefix([start_list, path_list]))
512
513 rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
514 if not rel_list:
515 return curdir
516 return join(*rel_list)
517 except (TypeError, AttributeError, BytesWarning, DeprecationWarning):
518 genericpath._check_arg_types('relpath', path, start)
519 raise
520
521
522# Return the longest common sub-path of the sequence of paths given as input.

Callers

nothing calls this directly

Calls 4

commonprefixFunction · 0.85
abspathFunction · 0.70
joinFunction · 0.70
splitMethod · 0.45

Tested by

no test coverage detected