Skip to content

Commit a01ba33

Browse files
girtsfmiss-islington
authored andcommitted
bpo-30618: add readlink to pathlib.Path (GH-8285)
This adds a "readlink" method to pathlib.Path objects that calls through to os.readlink. https://bugs.python.org/issue30618 Automerge-Triggered-By: @gpshead
1 parent 01659ca commit a01ba33

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

Doc/library/pathlib.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,19 @@ call fails (for example because the path doesn't exist).
944944
.. versionadded:: 3.5
945945

946946

947+
.. method:: Path.readlink()
948+
949+
Return the path to which the symbolic link points (as returned by
950+
:func:`os.readlink`)::
951+
952+
>>> p = Path('mylink')
953+
>>> p.symlink_to('setup.py')
954+
>>> p.readlink()
955+
PosixPath('setup.py')
956+
957+
.. versionadded:: 3.9
958+
959+
947960
.. method:: Path.rename(target)
948961

949962
Rename this file or directory to the given *target*, and return a new Path
@@ -1153,6 +1166,7 @@ os and os.path pathlib
11531166
:func:`os.path.isdir` :meth:`Path.is_dir`
11541167
:func:`os.path.isfile` :meth:`Path.is_file`
11551168
:func:`os.path.islink` :meth:`Path.is_symlink`
1169+
:func:`os.readlink` :meth:`Path.readlink`
11561170
:func:`os.stat` :meth:`Path.stat`,
11571171
:meth:`Path.owner`,
11581172
:meth:`Path.group`

Doc/whatsnew/3.9.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ customization consistently by always using the value specified by
150150
case), and one used ``__VENV_NAME__`` instead.
151151
(Contributed by Brett Cannon in :issue:`37663`.)
152152

153+
pathlib
154+
-------
155+
156+
Added :meth:`~pathlib.Path.readlink()` which acts similar to
157+
:func:`~os.readlink`.
158+
(Contributed by Girts Folkmanis in :issue:`30618`)
159+
153160
pprint
154161
------
155162

Lib/pathlib.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,15 @@ def write_text(self, data, encoding=None, errors=None):
12441244
with self.open(mode='w', encoding=encoding, errors=errors) as f:
12451245
return f.write(data)
12461246

1247+
def readlink(self):
1248+
"""
1249+
Return the path to which the symbolic link points.
1250+
"""
1251+
path = self._accessor.readlink(self)
1252+
obj = self._from_parts((path,), init=False)
1253+
obj._init(template=self)
1254+
return obj
1255+
12471256
def touch(self, mode=0o666, exist_ok=True):
12481257
"""
12491258
Create this file with the given access mode, if it doesn't exist.

Lib/test/test_pathlib.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,6 +1812,16 @@ def test_replace(self):
18121812
self.assertEqual(os.stat(r).st_size, size)
18131813
self.assertFileNotFound(q.stat)
18141814

1815+
@support.skip_unless_symlink
1816+
def test_readlink(self):
1817+
P = self.cls(BASE)
1818+
self.assertEqual((P / 'linkA').readlink(), self.cls('fileA'))
1819+
self.assertEqual((P / 'brokenLink').readlink(),
1820+
self.cls('non-existing'))
1821+
self.assertEqual((P / 'linkB').readlink(), self.cls('dirB'))
1822+
with self.assertRaises(OSError):
1823+
(P / 'fileA').readlink()
1824+
18151825
def test_touch_common(self):
18161826
P = self.cls(BASE)
18171827
p = P / 'newfileA'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :meth:`~pathlib.Path.readlink`. Patch by Girts Folkmanis.

0 commit comments

Comments
 (0)