我想递归地更改一个目录的组名,我正在使用 os.chown() 来做到这一点。但是我在 os.chown() 中找不到像 (chgrp -R) 这样的递归标志。
问问题
2164 次
2 回答
4
为什么不直接将命令传递给 shell?
os.system("chgrp -R ...")
于 2017-10-21T10:04:46.320 回答
2
我写了一个函数来执行 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)
于 2017-10-21T10:19:42.737 回答