Python | os.path.islink() method
Last Updated :
12 Jun, 2019
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:
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
import os.path
path = "/home/ihritik/Documents/file(original).txt"
isLink = os.path.islink(path)
print (isLink)
path = "/home/ihritik/Desktop/file(shortcut).txt"
isLink = os.path.islink(path)
print (isFile)
|
Reference: https://docs.python.org/3/library/os.path.html
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...