all Python 3osfunction
os.lstat
os — file system
Like stat, but does not follow symlinks (describes the link itself).
Syntax
os.lstat(path)
Use it to tell a link from what it points to: stat.S_ISLNK(os.lstat(p).st_mode). It works on a broken link, where os.stat would raise FileNotFoundError. For a regular file the result equals os.stat. os.stat(p, follow_symlinks=False) gives the same result.
Examples
import os
import stat
print(os.lstat(os.__file__).st_size > 0) # → True
print(os.lstat(os.__file__).st_size == os.stat(os.__file__).st_size) # → True — на обычном файле lstat и stat дают одно и то же
os.symlink(os.__file__, 'ссылка-на-os.py') # → создана символическая ссылка (на Windows нужны права разработчика)
print(stat.S_ISLNK(os.lstat('ссылка-на-os.py').st_mode), stat.S_ISLNK(os.stat('ссылка-на-os.py').st_mode)) # → True False — lstat видит саму ссылку, stat идёт по ней до цели
os.remove('ссылка-на-os.py') # → временная ссылка удалена
print(os.lstat('нет-такого-файла.txt')) # → FileNotFoundError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy