4

I know how to implement chmod u+w with the following code:

st = os.stat(dest_file)
os.chmod(dest_file, st.st_mode | stat.S_IWUSR)

But how about u-w?

1 Answer 1

6
st = os.stat(dest_file)
os.chmod(dest_file, st.st_mode & ~stat.S_IWUSR)

Explanation: ~ is the bitwise NOT operator, so a bitwise AND with ~stat.S_IWUSR clears the flag from st.st_mode.

To illustrate with imaginary values:

stat.S_IWUSR                    00001000
~stat.S_IWUSR                   11110111
s.st_mode                       00101001
s.st_mode & ~stat.S_IWUSR       00100001 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.