all Python 3Linux · macOSosfunction
os.lchown
os — permissions and ownership
Like chown, but affects the symlink itself, not its target. Availability: Unix.
Syntax
os.lchown(...)
It changes the owner of the link itself, not of its target. That matters when restoring archives and copying trees: chown would follow the link and change the target's owner, possibly outside the tree being copied. os.chown(path, uid, gid, follow_symlinks=False) does the same.
Examples
import os
open('target.txt', 'w').close() # → ? файл-цель: без него os.chown по ссылке ниже упадёт с FileNotFoundError
os.symlink('target.txt', 'link.txt') # → ? только Unix: создаём символическую ссылку для опытов
os.lchown('link.txt', os.getuid(), os.getgid()) # → ? только Unix: меняется владелец САМОЙ ссылки, файл target.txt не тронут
os.chown('link.txt', os.getuid(), os.getgid()) # → ? только Unix: в отличие от lchown идёт ПО ссылке и правит владельца target.txt
print(os.lstat('link.txt').st_uid == os.getuid()) # → ? True: lstat, как и lchown, смотрит на саму ссылку, а не на цель
os.lchown('нет-такой-ссылки', -1, -1) # → FileNotFoundError (пара -1, -1 не меняет ничего, но путь всё равно проверяется)
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy