How do you do a simple “chmod +x” from within python?

Use os.stat() to get the current permissions, use | to OR the bits together, and use os.chmod() to set the updated permissions.

Example:

import os
import stat

st = os.stat('somefile')
os.chmod('somefile', st.st_mode | stat.S_IEXEC)

Leave a Comment