Home > Mobile >  Why does os.chmod yield NotImplementedError?
Why does os.chmod yield NotImplementedError?

Time:11-21

I’m trying to use Python to set file permissions in a Unix system.

My code looks approximately like this:

os.chmod('/foo/bar.txt', 0o644, follow_symlinks=False)

The code works fine on macOS Monterey with Python 3.10.8.

Oddly enough, in a Linux VM with Debian 11 and Python 3.9.2 it fails:

NotImplementedError: chmod: follow_symlinks unavailable on this platform

However, when I try os.supports_follow_symlinks it does not throw an exception.

Does anyone understand why this happens?

CodePudding user response:

Linux man page for chmod(2):

   * chmod() changes the mode of the file specified whose pathname
     is given in pathname, which is dereferenced if it is a symbolic
     link.

and

   AT_SYMLINK_NOFOLLOW
          If pathname is a symbolic link, do not dereference it:
          instead operate on the link itself.  This flag is not
          currently implemented.

and

   ENOTSUP
          (fchmodat()) flags specified AT_SYMLINK_NOFOLLOW, which is
          not supported.

So Linux doesn't support it. MacOS does - but in a counterintuitive way.

os.supports_follow_symlinks is a Python set of functions that support not following symlinks, it won't throw any exception because it's not a function. And on Linux, that set does not contain chmod(2).

  • Related