[英]Is there any equivalent of chgrp -R in python?
我想递归地更改一个目录的组名,我正在使用os.chown()来做到这一点。 但是我在os.chown()中找不到像(chgrp -R)这样的递归标志。
为什么不将命令传递给shell?
os.system("chgrp -R ...")
我写了一个函数来执行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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.