MCPcopy Create free account
hub / github.com/RustPython/RustPython / relpath

Function relpath

Lib/ntpath.py:737–781  ·  view source on GitHub ↗

Return a relative version of a path

(path, start=None)

Source from the content-addressed store, hash-verified

735supports_unicode_filenames = True
736
737def relpath(path, start=None):
738 """Return a relative version of a path"""
739 path = os.fspath(path)
740 if not path:
741 raise ValueError("no path specified")
742
743 if isinstance(path, bytes):
744 sep = b'\\'
745 curdir = b'.'
746 pardir = b'..'
747 else:
748 sep = '\\'
749 curdir = '.'
750 pardir = '..'
751
752 if start is None:
753 start = curdir
754 else:
755 start = os.fspath(start)
756
757 try:
758 start_abs = abspath(start)
759 path_abs = abspath(path)
760 start_drive, _, start_rest = splitroot(start_abs)
761 path_drive, _, path_rest = splitroot(path_abs)
762 if normcase(start_drive) != normcase(path_drive):
763 raise ValueError("path is on mount %r, start on mount %r" % (
764 path_drive, start_drive))
765
766 start_list = start_rest.split(sep) if start_rest else []
767 path_list = path_rest.split(sep) if path_rest else []
768 # Work out how much of the filepath is shared by start and path.
769 i = 0
770 for e1, e2 in zip(start_list, path_list):
771 if normcase(e1) != normcase(e2):
772 break
773 i += 1
774
775 rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
776 if not rel_list:
777 return curdir
778 return sep.join(rel_list)
779 except (TypeError, ValueError, AttributeError, BytesWarning, DeprecationWarning):
780 genericpath._check_arg_types('relpath', path, start)
781 raise
782
783
784# Return the longest common sub-path of the iterable of paths given as input.

Callers

nothing calls this directly

Calls 7

splitrootFunction · 0.90
isinstanceFunction · 0.85
lenFunction · 0.85
abspathFunction · 0.70
normcaseFunction · 0.70
splitMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected