递归设置文件权限的Python方式是什么?

时间:2021-07-24 16:45:37

What's the "python way" to recursively set the owner and group to files in a directory? I could just pass a 'chown -R' command to shell, but I feel like I'm missing something obvious.

什么是以递归方式将所有者和组设置为目录中的文件的“python方式”?我可以将'chown -R'命令传递给shell,但我觉得我错过了一些明显的东西。

I'm mucking about with this:

我正在捣乱这个:


import os  
path = "/tmp/foo"  
for root, dirs, files in os.walk(path):  
  for momo in dirs:  
    os.chown(momo, 502, 20)

This seems to work for setting the directory, but fails when applied to files. I suspect the files are not getting the whole path, so chown fails since it can't find the files. The error is:

这似乎适用于设置目录,但在应用于文件时失败。我怀疑文件没有得到整个路径,因此chown失败,因为它无法找到文件。错误是:

'OSError: [Errno 2] No such file or directory: 'foo.html'

'OSError:[Errno 2]没有这样的文件或目录:'foo.html'

What am I overlooking here?

我在这里俯瞰什么?

6 个解决方案

#1


39  

The dirs and files lists are all always relative to root - i.e., they are the basename() of the files/folders, i.e. they don't have a / in them (or \ on windows). You need to join the dirs/files to root to get their whole path if you want your code to work to infinite levels of recursion:

目录和文件列表都始终相对于root - 即,它们是文件/文件夹的basename(),即它们中没有/(或者在Windows上)。如果希望代码在无限级别的递归中工作,则需要将dirs / files连接到root以获取其完整路径:

import os  
path = "/tmp/foo"  
for root, dirs, files in os.walk(path):  
  for momo in dirs:  
    os.chown(os.path.join(root, momo), 502, 20)
  for momo in files:
    os.chown(os.path.join(root, momo), 502, 20)

I'm suprised the shutil module doesn't have a function for this.

我很惊讶shutil模块没有这个功能。

#2


5  

import os  
path = "/tmp/foo"  
for root, dirs, files in os.walk(path):  
  for momo in dirs:  
    os.chown(momo, 502, 20)
  for file in files:
     fname = os.path.join(root, file)
     os.chown(fname, aaa, bb)

substitute aaa and bb as you please

随便用aaa和bb代替

#3


4  

try os.path.join(root,momo) that will give you full path

尝试os.path.join(root,momo),它将为您提供完整路径

#4


2  

Here is a function i wrote that uses glob to recursively list files and change their permissions.

这是我写的一个函数,它使用glob来递归列出文件并更改其权限。

import os
import glob
def recursive_file_permissions(path,mode,uid=-1,gid=-1):
        '''
        Recursively updates file permissions on a given path.
        UID and GID default to -1, and mode is required
        '''
    for item in glob.glob(path+'/*'):
        if os.path.isdir(item):
            recursive_file_permissions(os.path.join(path,item),mode,uid,gid)
        else:
            try:
                os.chown(os.path.join(path,item),uid,gid)
                os.chmod(os.path.join(path,item),mode)
            except:
                print('File permissions on {0} not updated due to error.'.format(os.path.join(path,item)))

it's not perfect, but got me where I needed to be

它不是完美的,但让我到达我需要的地方

#5


1  

Don't forget the for f in files loop, either. Similarly, remember to os.path.join(root, f) to get the full path.

不要忘记for f in files循环。同样,请记住os.path.join(root,f)以获取完整路径。

#6


-2  

use os.lchown instead of os.chown for changing link themselves and files together.

使用os.lchown而不是os.chown将链接本身和文件一起更改。

#1


39  

The dirs and files lists are all always relative to root - i.e., they are the basename() of the files/folders, i.e. they don't have a / in them (or \ on windows). You need to join the dirs/files to root to get their whole path if you want your code to work to infinite levels of recursion:

目录和文件列表都始终相对于root - 即,它们是文件/文件夹的basename(),即它们中没有/(或者在Windows上)。如果希望代码在无限级别的递归中工作,则需要将dirs / files连接到root以获取其完整路径:

import os  
path = "/tmp/foo"  
for root, dirs, files in os.walk(path):  
  for momo in dirs:  
    os.chown(os.path.join(root, momo), 502, 20)
  for momo in files:
    os.chown(os.path.join(root, momo), 502, 20)

I'm suprised the shutil module doesn't have a function for this.

我很惊讶shutil模块没有这个功能。

#2


5  

import os  
path = "/tmp/foo"  
for root, dirs, files in os.walk(path):  
  for momo in dirs:  
    os.chown(momo, 502, 20)
  for file in files:
     fname = os.path.join(root, file)
     os.chown(fname, aaa, bb)

substitute aaa and bb as you please

随便用aaa和bb代替

#3


4  

try os.path.join(root,momo) that will give you full path

尝试os.path.join(root,momo),它将为您提供完整路径

#4


2  

Here is a function i wrote that uses glob to recursively list files and change their permissions.

这是我写的一个函数,它使用glob来递归列出文件并更改其权限。

import os
import glob
def recursive_file_permissions(path,mode,uid=-1,gid=-1):
        '''
        Recursively updates file permissions on a given path.
        UID and GID default to -1, and mode is required
        '''
    for item in glob.glob(path+'/*'):
        if os.path.isdir(item):
            recursive_file_permissions(os.path.join(path,item),mode,uid,gid)
        else:
            try:
                os.chown(os.path.join(path,item),uid,gid)
                os.chmod(os.path.join(path,item),mode)
            except:
                print('File permissions on {0} not updated due to error.'.format(os.path.join(path,item)))

it's not perfect, but got me where I needed to be

它不是完美的,但让我到达我需要的地方

#5


1  

Don't forget the for f in files loop, either. Similarly, remember to os.path.join(root, f) to get the full path.

不要忘记for f in files循环。同样,请记住os.path.join(root,f)以获取完整路径。

#6


-2  

use os.lchown instead of os.chown for changing link themselves and files together.

使用os.lchown而不是os.chown将链接本身和文件一起更改。