我想递归地更改一个目录的组名,我使用os.chown()来实现这一点。但是我在os.chown()中找不到像(chgrp -R)这样的递归标志。
发布于 2017-10-21 10:19:42
我编写了一个函数来执行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-21 10:04:46
你为什么不直接把你的命令传递给空壳呢?
os.system("chgrp -R ...")https://stackoverflow.com/questions/46862074
复制相似问题