Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中是否有chgrp-R的等价物?_Python_Python 2.7_Python 3.x_Python Os - Fatal编程技术网

python中是否有chgrp-R的等价物?

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.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

  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时,为什么会如此复杂?