Open In App

Python | os.link() method

Last Updated : 12 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

os.link() method in Python is used to create a hard link. This method creates a hard link pointing to the source named destination. In this article, we will see what is os link and the uses of os.link().

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

os.link() Method Syntax in Python

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

Parameters: 

  1. src: A path-like object representing the file system path. This is the source file path for which the hard link will be created 
  2. 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. 
  3. 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. 
  4. dst_dir_fd (optional): A file descriptor referring to a directory. 
  5. follow_symlinks (optional) : A Boolean value.

Return type: This method does not return any value.

OS Link Method in Python

Below are some examples by which we can understand about Python os link method:

Python OS Link Example

In this example, a hard link is created from the source file located at `/home/ihritik/file.txt` to a destination path `/home/ihritik/Desktop/file(link).txt`. After successfully creating the hard link, a confirmation message is printed.

Python3
import os

src = '/home/ihritik/file.txt'

# Destination file path
dst = '/home/ihritik/Desktop/file(link).txt'

os.link(src, dst)

print("Hard link created successfully")

Output
Hard link created successfully

Checking the Existence of a File Before Creating a Hard Link

In this example, the script checks if a specified source file exists. If it does, a hard link is created to it at a given path; otherwise, an appropriate message is displayed indicating the file's absence.

Python3
import os

source_file = "path/to/source_file.txt"
hard_link_path = "path/to/hard_link.txt"

# Check if the source file exists before creating a hard link
if os.path.exists(source_file):
    os.link(source_file, hard_link_path)
    print(f"Hard link created from {source_file} to {hard_link_path}")
else:
    print(f"The source file {source_file} does not exist.")

Output
The source file path/to/source_file.txt does not exist.

Next Article
Article Tags :
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy