You can use os.chmod
but that'll only change permissions of that individual file or directory. To change permissions of a single file incrementally (by adding to the existing permissions), use:
import os
import stat
st = os.stat("path/to/file")
os.chmod("/path/to/file", st.st_mode | stat.S_IXUSR)
Now, to change permissions of files in a directory recursively, you would need to use os.walk
to traverse through all sub directories and their files:
import os
import stat
for root, dirs, files in os.walk("path/to/directory"):
for name in dirs:
path = os.path.join(root, name)
st = os.stat(path)
os.chmod(path, st.st_mode | stat.S_IXUSR)
for name in files:
path = os.path.join(root, name)
st = os.stat(path)
os.chmod(path, st.st_mode | stat.S_IXUSR)
The above will both traverse subdirectories and will incrementally modify permissions only to add user execution permissions.
There are other alternatives, one fun and obvious one is:
os.system("chmod -R u+x /path/to/file")
For future reference, here are two related questions: