Python | os.path.islink() 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.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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

False
True

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



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.