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().
Asked
Active
Viewed 3,201 times
2
-
Correct. Recurse manually. – Ignacio Vazquez-Abrams Oct 21 '17 at 09:59
-
So I must os.walk and change every files group using os.chown() ? – Fuji Komalan Oct 21 '17 at 10:02
-
@FujiClado Yes, – cs95 Oct 21 '17 at 10:04
-
I wrote a function to perform chgrp -R. – Fuji Komalan Oct 21 '17 at 10:22
2 Answers
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
-
Why so complicated when one could just pass the chgpr -R to the shell? – mfnalex Oct 31 '17 at 10:45