Open In App

Python | os.path.islink() method

Last Updated : 12 Jun, 2019
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. os.path module is sub module of OS module in Python used for common path name manipulation.

os.path.islink() method in Python is used to check whether the given path represents an existing directory entry that is a symbolic link or not.

Note: If symbolic links are not supported by the Python runtime then os.path.islink() method always returns False.

Syntax: os.path.islink(path)

Parameter:
path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.

Return Type: This method returns a Boolean value of class bool. This method returns True if the given path is a directory entry that is a symbolic link, otherwise returns False.

Create a soft link or symbolic link
In Unix or Linux, soft link or symbolic link can be created using ln command. Below is the syntax to create a symbolic link at the shell prompt:

$ ln -s {source-filename} {symbolic-filename}

Example:
create symbolic link

In above output, file(shortcut).txt is a symbolic link which also shows the file name to which it is linked.

Code: Use of os.path.islink() method to check if the given path is a symbolic link




# Python program to explain os.path.islink() method 
    
# importing os.path module 
import os.path
  
# Path 
path = "/home/ihritik/Documents/file(original).txt"
  
# Check whether the 
# given path is a
# symbolic link
isLink = os.path.islink(path)
print(isLink)
  
  
# Path
path = "/home/ihritik/Desktop/file(shortcut).txt"
  
  
# Check whether the 
# given path is a
# symbolic link
isLink = os.path.islink(path)
print(isFile)


Output:

False
True

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


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

Similar Reads