【发布时间】:2009-04-01 10:35:37
【问题描述】:
我想要一个给定目录的 python 程序,它将返回该目录中具有 775 (rwxrwxr-x) 权限的所有目录
谢谢!
【问题讨论】:
我想要一个给定目录的 python 程序,它将返回该目录中具有 775 (rwxrwxr-x) 权限的所有目录
谢谢!
【问题讨论】:
一定是python吗?
您也可以使用 find 来做到这一点:
"找到 .-perm 775"
【讨论】:
这两个答案都不会重复,尽管这并不完全清楚这就是 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.
【讨论】:
基于 Brian 回答的紧凑型生成器:
import os
(fpath for fpath
in (os.path.join(dirname,fname) for fname in os.listdir(dirname))
if (os.path.isdir(fpath) and (os.stat(fpath).st_mode & 0777) == 0775))
【讨论】:
您可以使用以下命令检查文件和目录的 775 权限
path="location of file or directory"
if (oct(os.stat(path).st_mode)[-3:]=="775"):
print(path)
【讨论】: