Why is the argument of os.umask() inverted? (umask 0o000 makes chmod 0o777) Why is the argument of os.umask() inverted? (umask 0o000 makes chmod 0o777) unix unix

Why is the argument of os.umask() inverted? (umask 0o000 makes chmod 0o777)


There is no real inconsistency, as the relation between umask and chmod can purely be written down with equations. Apparently, umask sets the opposite of chmod, it was created like this back in the old days.

Example: 022 (the default usual umask) creates 755. It works like this:

  • 7 - 0 = 7 becomes the first byte
  • 7 - 2 = 5 becomes the second and third bytes

Using this example, umask 777 creates a file with chmod 000, umask 112 will be equal to chmod 664. As far as I know, this happened because the umask command was originally created to indicate what permission bits the file will NOT have after it's created (hence the invertion).

While it could be annoying, it's really not hard to get used to it. Just think how you would chmod your files, and subtract the byte you want from 7, and you will get the umask value. Or, when you are at the IDE, writing your code, don't use umask, but rather create the file (with the default umask of course) and then use, in Python, os.chmod() instead.