Search code examples
pythonlinuxpermissionsoperating-systemfile-permissions

Change permission for directories recursively from drwxr-xr-x to drwxrwsrwx (setting setgid for the group users)


I am looking out for a way in python for adding special permissions for the folders recursively in python, I can find them mostly on linux commands but not with python.

I need to change permission for the groups users to setguid for directories recursively .


Solution

  • This works with python-3.10.9:

    import os;
    import stat;
    
    for dirname in ([x[0] for x in os.walk("root")]):
        os.chmod(dirname, os.stat(dirname).st_mode | stat.S_ISGID);
    

    Substitute "root" with name of directory to start from.

    I'm not experiensed with python, so maybe it is possible to simplify the code.