Home > Back-end >  How do I change group execute permissions while preserving every other permission using os.chmod?
How do I change group execute permissions while preserving every other permission using os.chmod?

Time:08-17

I'm trying to get my code to change the group executability permission using os.chmod while keeping all the other permissions in their original state, but I can't seem to figure out how.

All the chmod numbers change all the permissions. I want my program to change the group executability permission to True if it's currently False, and to False if it's currently True

CodePudding user response:

In order to "flip" the group execute bit you could do this:

import os

os.chmod(filename, os.stat(filename).st_mode ^ 0o10)

CodePudding user response:

stat.S_IMODE(os.stat(your_file).st_mode)

"Return the portion of the file’s mode that can be set by os.chmod() —that is, the file’s permission bits, plus the sticky bit, set-group-id, and set-user-id bits (on systems that support them)."

You can then check what the current permissions are, and modify them as needed.

  • Related