题
我想,给定的目录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 。
其他提示
根据布莱恩的回答紧凑型发生器:
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))
它是否必须蟒?
您还可以使用发现做到这一点:
“中找到。-perm 775”
不隶属于 StackOverflow