我想要一个给出目录的python程序,它将返回该目录中具有775(rwxrwxr-x
)权限的所有目录
谢谢!
答案 0 :(得分:8)
这两个答案都没有得到解决,尽管并不完全清楚这是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
答案 1 :(得分:5)
import os
for filename in os.listdir(dirname):
path=os.path.join(dirname, filename)
if os.path.isdir(path):
if (os.stat(path).st_mode & 0777) == 0775:
print path
答案 2 :(得分:2)
紧凑型发电机基于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))
答案 3 :(得分:0)
它必须是python吗?
你也可以使用find来做到这一点:
“找。-perm 775”
答案 4 :(得分:0)
您可以使用以下命令检查775文件和目录的权限
ValueError: Wrong number of columns at line 2