What I basically would like to do is cp -Rl dir1 dir2
. But as I understand it, python only provides shutils.copytree(src,dst)
which actually copies the files, but has no possibility of hardlinking the files instead.
I know that I could invoke the cp
command using the subprocess
module, but I'd rather like to find a cleaner (pythonic) way to do so.
So is there an easy way to do so or do I have to implement it myself recursing through the directories?
link() method in Python is used to create a hard link. This method creates a hard link pointing to the source named destination.
copytree() method recursively copies an entire directory tree rooted at source (src) to the destination directory. The destination directory, named by (dst) must not already exist. It will be created during copying.
You can copy the contents of one folder to another using the shutil. copy(), shutil. copy2() and shutil. copytree() methods of this module.
You just have to call os.system("cp -Rl dir1 dir2")
, no need hand write your own function.
Edited: Since you want do this in python.
You are right: It's available in module shutil
:
shutil.copytree(src, dst, copy_function=os.link)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With