Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从'-rw-r--r--';回到33188?_Python_Unix_File Permissions_Stat_Bitmask - Fatal编程技术网

Python 如何从'-rw-r--r--';回到33188?

Python 如何从'-rw-r--r--';回到33188?,python,unix,file-permissions,stat,bitmask,Python,Unix,File Permissions,Stat,Bitmask,Python有一个helper函数,可以从所报告的st_模式(integer)转换为熟悉的“stringy”格式(我不知道这个表示是否有正确的名称) 是否有任何“unflemode”辅助函数可以反其道而行 >>> unfilemode('-rw-r--r--') 33188 这是我尝试过的,但它产生了错误的结果。这不是正确处理表示文件类型的第一个字符,也不是处理粘性位等 table = { ord('r'): '1', ord('w'): '1', o

Python有一个helper函数,可以从所报告的
st_模式(integer)转换为熟悉的“stringy”格式(我不知道这个表示是否有正确的名称)

是否有任何“unflemode”辅助函数可以反其道而行

>>> unfilemode('-rw-r--r--')
33188
这是我尝试过的,但它产生了错误的结果。这不是正确处理表示文件类型的第一个字符,也不是处理粘性位等

table = {
    ord('r'): '1',
    ord('w'): '1',
    ord('x'): '1',
    ord('-'): '0',
}

def unfilemode(s):
    return int(s.translate(table), 2)

Python是开源的,您只需读取
stat
模块的源代码并编写反向函数即可

见:

请注意,我正在“调皮”,访问
stat
模块的私有成员。通常的警告适用

另请注意,
stat.filemode
的文档无论如何都是不正确的,因为
0o100000
在技术上不是文件模式的一部分,而是文件类型
S_IFREG
。发件人:

POSIX将与掩码
S_IFMT
(见下文)对应的stat.st_模式位作为文件类型,将与掩码07777对应的12位作为文件模式位,将最低有效9位(0777)作为文件权限位


你从哪里得到字符串,它能为你提供另一种格式吗?在我的平台上,filemode的Python实现被C实现的Python实现吹走了,每个实现的行为都有所不同。图..@wim:这就解释了为什么它工作正常,因为Python代码没有。
table = {
    ord('r'): '1',
    ord('w'): '1',
    ord('x'): '1',
    ord('-'): '0',
}

def unfilemode(s):
    return int(s.translate(table), 2)
import stat

def un_filemode(mode_str):
    mode = 0
    for char, table in zip(mode_str, stat._filemode_table):
        for bit, bitchar in table:
            if char == bitchar:
                mode |= bit
                break
    return mode