Skip to content

Commit 6b5b013

Browse files
nanjekyejoannahpitrou
authored andcommitted
bpo-26978: Implement pathlib.Path.link_to (Using os.link) (GH-12990)
1 parent f090019 commit 6b5b013

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

Doc/library/pathlib.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,13 @@ call fails (for example because the path doesn't exist).
10541054
use :func:`Path.rmdir` instead.
10551055

10561056

1057+
.. method:: Path.link_to(target)
1058+
1059+
Create a hard link pointing to a path named *target*.
1060+
1061+
.. versionchanged:: 3.8
1062+
1063+
10571064
.. method:: Path.write_bytes(data)
10581065

10591066
Open the file pointed to in bytes mode, write *data* to it, and close the

Doc/whatsnew/3.8.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ pathlib
369369
contain characters unrepresentable at the OS level.
370370
(Contributed by Serhiy Storchaka in :issue:`33721`.)
371371

372+
Added :meth:`pathlib.Path.link_to()` which creates a hard link pointing
373+
to a path.
374+
(Contributed by Joannah Nanjekye in :issue:`26978`)
375+
372376

373377
socket
374378
------

Lib/pathlib.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ def lchmod(self, pathobj, mode):
411411

412412
unlink = os.unlink
413413

414+
link_to = os.link
415+
414416
rmdir = os.rmdir
415417

416418
rename = os.rename
@@ -1303,6 +1305,14 @@ def lstat(self):
13031305
self._raise_closed()
13041306
return self._accessor.lstat(self)
13051307

1308+
def link_to(self, target):
1309+
"""
1310+
Create a hard link pointing to a path named target.
1311+
"""
1312+
if self._closed:
1313+
self._raise_closed()
1314+
self._accessor.link_to(self, target)
1315+
13061316
def rename(self, target):
13071317
"""
13081318
Rename this path to the given path.

Lib/test/test_pathlib.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,25 @@ def test_rmdir(self):
16431643
self.assertFileNotFound(p.stat)
16441644
self.assertFileNotFound(p.unlink)
16451645

1646+
def test_link_to(self):
1647+
P = self.cls(BASE)
1648+
p = P / 'fileA'
1649+
size = p.stat().st_size
1650+
# linking to another path.
1651+
q = P / 'dirA' / 'fileAA'
1652+
try:
1653+
p.link_to(q)
1654+
except PermissionError as e:
1655+
self.skipTest('os.link(): %s' % e)
1656+
self.assertEqual(q.stat().st_size, size)
1657+
self.assertEqual(os.path.samefile(p, q), True)
1658+
self.assertTrue(p.stat)
1659+
# Linking to a str of a relative path.
1660+
r = rel_join('fileAAA')
1661+
q.link_to(r)
1662+
self.assertEqual(os.stat(r).st_size, size)
1663+
self.assertTrue(q.stat)
1664+
16461665
def test_rename(self):
16471666
P = self.cls(BASE)
16481667
p = P / 'fileA'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`pathlib.path.link_to()` is now implemented. It creates a hard link pointing
2+
to a path.

0 commit comments

Comments
 (0)