Python Practice › OS Module Programs › Program 47
Is a path a symbolic link?
Write a Python program to check whether a path is a symbolic link.
Output
Creating symlinks needs extra permission here target.txt is a link: False
Approach
os.path.islink; creating links may need extra rights on Windows, so handle the error.
Solution
import os open("target.txt", "w").close()try: if not os.path.lexists("link.txt"): os.symlink("target.txt", "link.txt") print("link.txt is a link:", os.path.islink("link.txt"))except OSError: print("Creating symlinks needs extra permission here")print("target.txt is a link:", os.path.islink("target.txt"))Output
Creating symlinks needs extra permission here target.txt is a link: False
Learn the concept
New to this topic? Read the lesson, then come back.