Computer >> Computer tutorials >  >> Programming >> Python

How to create hardlink of a file using Python?


The method os.link(src, dst) creates a hard link pointing to src named dst. This method is very useful to create a copy of existing file.

Example

For example, if you have a file called photo.jpg and want to create a  hard link to it called my_photo.jpg, then you could simply use:

>>> import os
>>> os.link('photo.jpg', 'my_photo.jpg')

 Now if you list the files in that directory, you'll get the my_photo