Sunday, August 31, 2025
HomeLanguagesPython | os.symlink() method

Python | os.symlink() 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.symlink() method in Python is used to create symbolic link. This method creates symbolic link pointing to source named destination.
To read about symbolic links/soft links, please refer to this article.

Syntax: os.symlink(src, dst, target_is_directory = False, *, dir_fd = None)

Parameters:
src: A path-like object representing the file system path. This is the source file path for which the symbolic link will be created.
dst: A path-like object representing the file system path. This is the target file path where symbolic link will be created.
target_is_directory (optional): The default value of this parameter is False. If the specified target path is directory then its value should be True.
dir_fd (optional): A file descriptor referring to a directory.

Return type: This method does not return any value.

Code: Use of os.symlink() method




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


Output:

Symbolic link created successfully

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

RELATED ARTICLES

Most Popular

Dominic
32250 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6619 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11840 POSTS0 COMMENTS
Shaida Kate Naidoo
6734 POSTS0 COMMENTS
Ted Musemwa
7014 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6704 POSTS0 COMMENTS