Home > Software design >  Removing permissions using chmod
Removing permissions using chmod

Time:08-13

So for this I am trying to change only the group permissions for a file using chmod. I was able to add a executable permission to the group using

permissions = os.stat(filepath)
os.chmod(filepath, permissions.st_mode | stat.S_IXGRP)

I added the execute permission to the group successfully using this code but I don't know how to remove the permission if needed. Can anyone help me with this.

CodePudding user response:

You don't remove a permission. You set a new permission. So you can set the permission to read or read write; those variants don't have executable permission.

If you AND (&) the current permission with a combined read write permission, it should effectively remove the execute permission. So,

os.chmod(filepath, permissions.st_mmode & (stat.IRGRP | stat.IWGRP)

Only the common modes will remain in the second argument, and any execute permission will be removed.

Disclaimer: I haven't checked nor used this, but if it's all bitmasks, this should work.

  • Related