Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-34526:[doc] Add description and examples of multiple arguments for Path.relative_to #31368

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,10 @@ Pure paths provide the following methods and properties:

.. method:: PurePath.relative_to(*other)

Compute a version of this path relative to the path represented by
*other*. If it's impossible, ValueError is raised::
Return the relative path to another path identified by *other*, each
element of *other* can be either a string representing a path segment,
or another path object implementing the :class:`PurePath`(or a subclass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. You missed the definition of class PurePath in the context, it could be changed from :class:PurePath to the class PurePath.

of :class:`PurePath`). If the operation is impossible, ValueError is raised::

>>> p = PurePosixPath('/etc/passwd')
>>> p.relative_to('/')
Expand All @@ -555,6 +557,19 @@ Pure paths provide the following methods and properties:
.format(str(self), str(formatted)))
ValueError: '/etc/passwd' is not in the subpath of '/usr' OR one path is relative and the other absolute.

If *other* contains more than one element, rules for concatenation of
all elements is similarly to :class:`PurePath`::

>>> p = PurePosixPath('/tmp/foo/bar')
>>> p.relative_to('/tmp', 'foo')
PurePosixPath('bar')
>>> p.relative_to('/tmp', 'bar')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pathlib.py", line 816, in relative_to
raise ValueError("{!r} is not in the subpath of {!r}"
ValueError: '/tmp/foo/bar' is not in the subpath of '/tmp/bar' OR one path is relative and the other is absolute.

NOTE: This function is part of :class:`PurePath` and works with strings. It does not check or access the underlying file structure.


Expand Down