Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing permissions using chmod

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading