Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively creating hardlinks using python

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?

like image 245
devsnd Avatar asked May 28 '12 00:05

devsnd


People also ask

How do you create a hard link in Python?

link() method in Python is used to create a hard link. This method creates a hard link pointing to the source named destination.

How do I copy a directory in Python recursively?

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.

How do I copy a folder to another directory in Python?

You can copy the contents of one folder to another using the shutil. copy(), shutil. copy2() and shutil. copytree() methods of this module.


1 Answers

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)
like image 102
Kabie Avatar answered Oct 20 '22 05:10

Kabie