ID EN
Pathlib

relative_to

Python 3.11 🇮🇩 Bahasa Indonesia

Hitung versi jalur ini relatif terhadap jalur yang diwakili oleh jalur lainnya. Jika tidak memungkinkan, ValueError dimunculkan:

Syntax

PYTHON
PurePath.relative_to(*other)

Contoh

Example 1
PYTHON
>>> p = PurePosixPath('/etc/passwd')
>>> p.relative_to('/')
PurePosixPath('etc/passwd')
>>> p.relative_to('/etc')
PurePosixPath('passwd')
>>> p.relative_to('/usr')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pathlib.py", line 694, in relative_to
    .format(str(self), str(formatted)))
ValueError: '/etc/passwd' is not in the subpath of '/usr' OR one path is relative and the other absolute.

See Also

PurePath