gpt4 book ai didi

python - 检查python中目录的权限

转载 作者:行者123 更新时间:2023-11-28 19:36:29 33 4
gpt4 key购买 nike

我想要一个给定目录的 python 程序,它将返回该目录中具有 775 (rwxrwxr-x) 权限的所有目录

谢谢!

最佳答案

这两个答案都没有递归,尽管还不完全清楚这就是 OP 想要的。这是一种递归方法(未经测试,但您明白了):

import os
import stat
import sys

MODE = "775"

def mode_matches(mode, file):
"""Return True if 'file' matches 'mode'.

'mode' should be an integer representing an octal mode (eg
int("755", 8) -> 493).
"""
# Extract the permissions bits from the file's (or
# directory's) stat info.
filemode = stat.S_IMODE(os.stat(file).st_mode)

return filemode == mode

try:
top = sys.argv[1]
except IndexError:
top = '.'

try:
mode = int(sys.argv[2], 8)
except IndexError:
mode = MODE

# Convert mode to octal.
mode = int(mode, 8)

for dirpath, dirnames, filenames in os.walk(top):
dirs = [os.path.join(dirpath, x) for x in dirnames]
for dirname in dirs:
if mode_matches(mode, dirname):
print dirname

stdlib 文档中描述了类似的内容 stat .

关于python - 检查python中目录的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/704945/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com