all Python 3statfunction
stat.S_ISLNK()
file type
Checks from st_mode that the object is a symbolic link. It only works with a mode taken from os.lstat().
Syntax
stat.S_ISLNK(os.lstat(path).st_mode)
The main trap: os.stat() follows the link and returns the target's mode, so S_ISLNK(os.stat(p).st_mode) is always False. Use os.lstat(p), os.stat(p, follow_symlinks=False) or DirEntry.stat(follow_symlinks=False). On Windows, creating a link with os.symlink() needs Developer Mode or administrator rights.
Examples
import os, stat
open('target.txt', 'w').close()
os.symlink('target.txt', 'link.txt')
print(stat.S_ISLNK(os.stat('link.txt').st_mode)) # → False — stat() прошёл по ссылке
print(stat.S_ISLNK(os.lstat('link.txt').st_mode)) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy