MCPcopy Index your code
hub / github.com/RustPython/RustPython / relpath

Function relpath

Lib/posixpath.py:513–548  ·  view source on GitHub ↗

Return a relative version of a path

(path, start=None)

Source from the content-addressed store, hash-verified

511supports_unicode_filenames = (sys.platform == 'darwin')
512
513def relpath(path, start=None):
514 """Return a relative version of a path"""
515
516 path = os.fspath(path)
517 if not path:
518 raise ValueError("no path specified")
519
520 if isinstance(path, bytes):
521 curdir = b'.'
522 sep = b'/'
523 pardir = b'..'
524 else:
525 curdir = '.'
526 sep = '/'
527 pardir = '..'
528
529 if start is None:
530 start = curdir
531 else:
532 start = os.fspath(start)
533
534 try:
535 start_tail = abspath(start).lstrip(sep)
536 path_tail = abspath(path).lstrip(sep)
537 start_list = start_tail.split(sep) if start_tail else []
538 path_list = path_tail.split(sep) if path_tail else []
539 # Work out how much of the filepath is shared by start and path.
540 i = len(commonprefix([start_list, path_list]))
541
542 rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
543 if not rel_list:
544 return curdir
545 return sep.join(rel_list)
546 except (TypeError, AttributeError, BytesWarning, DeprecationWarning):
547 genericpath._check_arg_types('relpath', path, start)
548 raise
549
550
551# Return the longest common sub-path of the sequence of paths given as input.

Callers

nothing calls this directly

Calls 7

isinstanceFunction · 0.85
lenFunction · 0.85
commonprefixFunction · 0.85
abspathFunction · 0.70
lstripMethod · 0.45
splitMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected