Open In App

Python os.symlink() method

Last Updated : 15 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

You can use the os.symlink() method in Python to create a symbolic link that points to the given source. Symbolic link creates a shortcut referencing to the original file, without creating its copy.

Syntax os.symlink() method in python:

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

Parameters:

  • src: This is the source file path for which the symbolic link will be created. 
  • dst: 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.

To read about symbolic links/soft links, please refer to this article

Example: Using Python os.symlink() method 

Let’s see the practical use of os.symlink() method by writing a Python program.

Python3
# 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

We have covered how to create symbolic links in Python using os.symlink method. Symbolic links are used to create logical links without physical file copies, this is very useful in managing files and directories.

We have explained the os.symlink method in easy words with an example program, you can easily learn and practice os.symlink() method using our guide.

Learn More on OS Module Methods


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads