use :func:`Path.rmdir` instead.
+.. method:: Path.link_to(target)
+
+ Create a hard link pointing to a path named *target*.
+
+ .. versionchanged:: 3.8
+
+
.. method:: Path.write_bytes(data)
Open the file pointed to in bytes mode, write *data* to it, and close the
contain characters unrepresentable at the OS level.
(Contributed by Serhiy Storchaka in :issue:`33721`.)
+Added :meth:`pathlib.Path.link_to()` which creates a hard link pointing
+to a path.
+(Contributed by Joannah Nanjekye in :issue:`26978`)
+
socket
------
unlink = os.unlink
+ link_to = os.link
+
rmdir = os.rmdir
rename = os.rename
self._raise_closed()
return self._accessor.lstat(self)
+ def link_to(self, target):
+ """
+ Create a hard link pointing to a path named target.
+ """
+ if self._closed:
+ self._raise_closed()
+ self._accessor.link_to(self, target)
+
def rename(self, target):
"""
Rename this path to the given path.
self.assertFileNotFound(p.stat)
self.assertFileNotFound(p.unlink)
+ def test_link_to(self):
+ P = self.cls(BASE)
+ p = P / 'fileA'
+ size = p.stat().st_size
+ # linking to another path.
+ q = P / 'dirA' / 'fileAA'
+ try:
+ p.link_to(q)
+ except PermissionError as e:
+ self.skipTest('os.link(): %s' % e)
+ self.assertEqual(q.stat().st_size, size)
+ self.assertEqual(os.path.samefile(p, q), True)
+ self.assertTrue(p.stat)
+ # Linking to a str of a relative path.
+ r = rel_join('fileAAA')
+ q.link_to(r)
+ self.assertEqual(os.stat(r).st_size, size)
+ self.assertTrue(q.stat)
+
def test_rename(self):
P = self.cls(BASE)
p = P / 'fileA'
--- /dev/null
+`pathlib.path.link_to()` is now implemented. It creates a hard link pointing
+to a path.