python中是否有chgrp-R的等价物?
我想递归地更改一个目录的组名,我正在使用os.chown()来实现这一点。但是我在os.chown()中找不到任何类似(chgrp-R)的递归标志。为什么不将命令传递给shell呢python中是否有chgrp-R的等价物?,python,python-2.7,python-3.x,python-os,Python,Python 2.7,Python 3.x,Python Os,我想递归地更改一个目录的组名,我正在使用os.chown()来实现这一点。但是我在os.chown()中找不到任何类似(chgrp-R)的递归标志。为什么不将命令传递给shell呢 os.system("chgrp -R ...") 你为什么不把命令传给shell呢 os.system("chgrp -R ...") 我编写了一个函数来执行chgrp-R def chgrp(LOCATION,OWNER,recursive=False): import os import grp
os.system("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)
我编写了一个函数来执行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)
对的。手动递归。所以我必须使用os.chown()操作os.walk并更改每个文件组?@FujiClado是的,我编写了一个函数来执行chgrp-R.Correct。手动递归。因此,我必须使用os.chown()操作os.walk并更改每个文件组?@FujiClado是的,我编写了一个函数来执行chgrp-R。当一个人可以将chgpr-R传递给shell时,为什么会如此复杂?当一个人可以将chgpr-R传递给shell时,为什么会如此复杂?