MCPcopy Index your code
hub / github.com/python/cpython / relative_to

Method relative_to

Lib/pathlib/__init__.py:478–501  ·  view source on GitHub ↗

Return the relative path to another path identified by the passed arguments. If the operation is not possible (because this is not related to the other path), raise ValueError. The *walk_up* parameter controls whether `..` may be used to resolve the path.

(self, other, *, walk_up=False)

Source from the content-addressed store, hash-verified

476 return ['.' + ext for ext in self.name.lstrip('.').split('.')[1:]]
477
478 def relative_to(self, other, *, walk_up=False):
479 """Return the relative path to another path identified by the passed
480 arguments. If the operation is not possible (because this is not
481 related to the other path), raise ValueError.
482
483 The *walk_up* parameter controls whether `..` may be used to resolve
484 the path.
485 """
486 if not hasattr(other, 'with_segments'):
487 other = self.with_segments(other)
488 parts = []
489 for path in chain([other], other.parents):
490 if path == self or path in self.parents:
491 break
492 elif not walk_up:
493 raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
494 elif path.name == '..':
495 raise ValueError(f"'..' segment in {str(other)!r} cannot be walked")
496 else:
497 parts.append('..')
498 else:
499 raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")
500 parts.extend(self._tail[len(path._tail):])
501 return self._from_parsed_parts('', '', parts)
502
503 def is_relative_to(self, other):
504 """Return True if the path is relative to another path or False.

Callers 15

gather_idsFunction · 0.45
mainFunction · 0.45
packageFunction · 0.45
clone_testbedFunction · 0.45
configure_wasi_pythonFunction · 0.45
make_wasi_pythonFunction · 0.45
create_archiveFunction · 0.45
_is_subpathFunction · 0.45
setUpClassMethod · 0.45
check_files_presentMethod · 0.45
_compile_importlibMethod · 0.45

Calls 6

with_segmentsMethod · 0.95
_from_parsed_partsMethod · 0.95
chainFunction · 0.85
strFunction · 0.85
appendMethod · 0.45
extendMethod · 0.45

Tested by 8

setUpClassMethod · 0.36
check_files_presentMethod · 0.36
_compile_importlibMethod · 0.36
test_relative_toMethod · 0.36
test_bytesMethod · 0.36
test_relative_toMethod · 0.36