Skip to content

bpo-30618: add readlink to pathlib.Path #8285

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

Merged
merged 1 commit into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,19 @@ call fails (for example because the path doesn't exist).
.. versionadded:: 3.5


.. method:: Path.readlink()

Return the path to which the symbolic link points (as returned by
:func:`os.readlink`)::

>>> p = Path('mylink')
>>> p.symlink_to('setup.py')
>>> p.readlink()
PosixPath('setup.py')

.. versionadded:: 3.9


.. method:: Path.rename(target)

Rename this file or directory to the given *target*, and return a new Path
Expand Down Expand Up @@ -1153,6 +1166,7 @@ os and os.path pathlib
:func:`os.path.isdir` :meth:`Path.is_dir`
:func:`os.path.isfile` :meth:`Path.is_file`
:func:`os.path.islink` :meth:`Path.is_symlink`
:func:`os.readlink` :meth:`Path.readlink`
:func:`os.stat` :meth:`Path.stat`,
:meth:`Path.owner`,
:meth:`Path.group`
Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ customization consistently by always using the value specified by
case), and one used ``__VENV_NAME__`` instead.
(Contributed by Brett Cannon in :issue:`37663`.)

pathlib
-------

Added :meth:`~pathlib.Path.readlink()` which acts similar to
:func:`~os.readlink`.
(Contributed by Girts Folkmanis in :issue:`30618`)

pprint
------

Expand Down
9 changes: 9 additions & 0 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,15 @@ def write_text(self, data, encoding=None, errors=None):
with self.open(mode='w', encoding=encoding, errors=errors) as f:
return f.write(data)

def readlink(self):
"""
Return the path to which the symbolic link points.
"""
path = self._accessor.readlink(self)
obj = self._from_parts((path,), init=False)
obj._init(template=self)
return obj

def touch(self, mode=0o666, exist_ok=True):
"""
Create this file with the given access mode, if it doesn't exist.
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,16 @@ def test_replace(self):
self.assertEqual(os.stat(r).st_size, size)
self.assertFileNotFound(q.stat)

@support.skip_unless_symlink
def test_readlink(self):
P = self.cls(BASE)
self.assertEqual((P / 'linkA').readlink(), self.cls('fileA'))
self.assertEqual((P / 'brokenLink').readlink(),
self.cls('non-existing'))
self.assertEqual((P / 'linkB').readlink(), self.cls('dirB'))
with self.assertRaises(OSError):
(P / 'fileA').readlink()

def test_touch_common(self):
P = self.cls(BASE)
p = P / 'newfileA'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :meth:`~pathlib.Path.readlink`. Patch by Girts Folkmanis.