all Python 3macOS · Windowsosfunction
os.lchmod()
permissions
Changes the mode of the symlink itself rather than its target; equivalent to os.chmod(path, mode, follow_symlinks=False). Available on macOS/BSD and on Windows (3.13+), not on Linux.
Syntax
os.lchmod(path, mode)
It does not exist on Linux at all: hasattr(os, 'lchmod') is false, and chmod(path, mode, follow_symlinks=False) raises NotImplementedError. The reason is that Linux does not use a link's own permissions. Portable code should check for the function or for os.chmod in os.supports_follow_symlinks.
Examples
import os, stat
print(hasattr(os, 'chmod')) # → True (os.chmod есть везде, os.lchmod — не везде)
print(callable(getattr(os, 'lchmod', os.chmod))) # → True (на Linux берём запасной os.chmod)
print(oct(stat.S_IMODE(0o100644))) # → 0o644 (mode задаётся восьмеричным числом)
print(stat.S_IMODE(0o100755) == 0o755) # → True
import os, stat
# os.lchmod(path, 0o644) ≡ os.chmod(path, 0o644, follow_symlinks=False)
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy