Python | os.link() method

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

os.link() method in Python is used to create a hard link. This method create a hard link pointing to source named destination.
To read about hard link, please refer this article.

Note: This method is only available on Windows and Unix platforms.



Syntax: os.link(src, dst, *, src_dir_fd = None, dst_dir_fd = None, follow_symlinks = True)

Parameters:
src: A path-like object representing the file system path. This is the source file path for which the hard link will be created
dst: A path-like object representing the file system path. This is the target file path where hard link will be created.
A path-like object is a string or bytes object which represents a path.
src_dir_fd (optional): A file descriptor referring to a directory.The default value of this parameter is None. If the specified src path is absolute then this parameter is ignored. If the specified src path is relative and src_dir_fd is not None then the specified src path is relative to the directory associated with src_dir_fd.
dst_dir_fd (optional): A file descriptor referring to a directory.
follow_symlinks (optional) : A Boolean value.

Return type: This method does not return any value.

Code: Use of os.link() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.link() method 
    
# importing os module 
import os
  
  
# Source file path
src = '/home/ihritik/file.txt'
  
# Destination file path
dst = '/home/ihritik/Desktop/file(link).txt'
  
# Create a hard link
# pointing to src named dst
# using os.link() method
# os.link(src, dst)
  
print("Hard link created successfully")

chevron_right


Output:

Hard link created successfully

Reference: https://docs.python.org/3/library/os.html#os.link



My Personal Notes arrow_drop_up

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.