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)
3条答案
按热度按时间waxmsbnn1#
你为什么不直接把命令传给shell呢?
字符串
anhgbhbe2#
我写了一个函数来执行chgrp -R
字符串
nhhxz33t3#
采用@fuji-komalan的解决方案,并做一些不同的事情:
字符串
通过使用
shutil.chown
,您不需要显式地查找组的gid(尽管只执行一次可能会有轻微的性能优势)。