Python Forum
makin hardlinks with pathlib.Path
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
makin hardlinks with pathlib.Path
#1
i see that pathlib.Path.symlink_to exists and is documented. so, i can create a symlink. but i see no means to do a hardlink. i would have expected something named pathlib.Path.link_to to be there to be that. but it does not exist nor is anything like a hardlink documented for pathlib. it looks like i will need to use os.link to make the hardlinks. is there a known way to make a hardlink with just pathlib?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
You can mokey patch.

import pathlib


def link(self, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True):
    os.link(self, dst, src_dir_fd=src_dir_fd, dst_dir_fd=dst_dir_fd, follow_symlinks=follow_symlinks)


pathlib.PosixPath.link = link
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
if you trace source code of pathlib
you will see that it actively uses os module. Internally, many methods of pathlib.Path class invokes
os.<some_method>. The latter (os), in turn, is based primarily on posixmodule.c.
So, it is possible to create hardlinks using pathlib (joke!), e.g.
import pathlib
pathlib.os.link(...) # will create hard link
... Unfortunately, I think, there is no way to do that the way you want.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pathlib import not working chriswrcg 9 3,650 May-29-2022, 07:37 PM
Last Post: snippsat
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,192 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  deleting an empty Dir, using pathlib.Path tester_V 9 5,762 Jul-01-2021, 01:53 PM
Last Post: Gribouillis
  Trying to pathlib instead of os.path tester_V 4 2,470 Jun-22-2021, 04:15 AM
Last Post: tester_V
  pathlib destpath.exists() true even file does not exist NaN 9 4,646 Dec-01-2020, 12:43 PM
Last Post: NaN
  Question about if with () or without () / pathlib Tecuma 3 2,198 Apr-02-2020, 10:02 AM
Last Post: Tecuma
  pathlib hanging bluefrog 2 3,112 Sep-25-2018, 12:59 PM
Last Post: volcano63
  pathlib: resolving a path that does not exist Skaperen 6 5,460 Sep-08-2018, 12:25 AM
Last Post: Skaperen
  Need help using pathlib to read text file into dictionary gwilli3 4 4,172 Aug-13-2018, 06:21 PM
Last Post: gwilli3
  How does pathlib.Path.rename work? wavic 7 16,892 Aug-02-2018, 10:58 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020