2

I want to change the group name of one directory recursively, I am using os.chown() to do that. But I can not find any recursive flag like (chgrp -R) in os.chown().

Fuji Komalan
  • 1,979
  • 16
  • 25

2 Answers2

4

Why dont you just pass your command to the shell?

os.system("chgrp -R ...")
mfnalex
  • 833
  • 5
  • 16
2

I wrote a function to perform chgrp -R

def chgrp(LOCATION,OWNER,recursive=False):

  import os 
  import grp

  gid = grp.getgrnam(OWNER).gr_gid
  if recursive:
      if os.path.isdir(LOCATION):
        os.chown(LOCATION,-1,gid)
        for curDir,subDirs,subFiles in os.walk(LOCATION):
          for file in subFiles:
            absPath = os.path.join(curDir,file)
            os.chown(absPath,-1,gid)
          for subDir in subDirs:
            absPath = os.path.join(curDir,subDir)
            os.chown(absPath,-1,gid)
      else:
       os.chown(LOCATION,-1,gid)
  else:
    os.chown(LOCATION,-1,gid)
Fuji Komalan
  • 1,979
  • 16
  • 25