+ -
当前位置:首页 → 问答吧 → 【请教】用stat得到文件权限?

【请教】用stat得到文件权限?

时间:2011-07-01

来源:互联网

python里执行命令:

>>>stat.S_IMODE(os.stat('/root/test').st_mode)
420

如何根据420这个值 得到 /root/test 的权限呢 ?

请大牛指教

作者: 清潼止水   发布时间: 2011-07-01

在线等。。。。

作者: 清潼止水   发布时间: 2011-07-01

自己摸索出来了:

>>> stat.S_IMODE(os.stat('/root/test').st_mode) & stat.S_IRUSR
256
>>> stat.S_IMODE(os.stat('/root/test').st_mode) & stat.S_IWUSR
128
>>> stat.S_IMODE(os.stat('/root/test').st_mode) & stat.S_IXUSR
0
>>> stat.S_IMODE(os.stat('/root/test').st_mode) & stat.S_IRGRP
32
>>> stat.S_IMODE(os.stat('/root/test').st_mode) & stat.S_IWGRP
0
>>> stat.S_IMODE(os.stat('/root/test').st_mode) & stat.S_IXGRP
0
>>> stat.S_IMODE(os.stat('/root/test').st_mode) & stat.S_IROTH
4
>>> stat.S_IMODE(os.stat('/root/test').st_mode) & stat.S_IWOTH
0
>>> stat.S_IMODE(os.stat('/root/test').st_mode) & stat.S_IXOTH
0

非0 表示有权限 0 表示没权限

[root@rhel_server ~]# ls -l test
-rw-r--r-- 1 root root 0 Jul  1 15:14 test

作者: 清潼止水   发布时间: 2011-07-01

回复 清潼止水
420  =>100     010     000  =>r--       -w-      ---

作者: 106033177   发布时间: 2011-07-01

回复 106033177

貌似不是这样(⊙o⊙)…,看看这个:

>>> stat.S_IMODE(os.stat('/root/test').st_mode)
420
>>> commands.getoutput('ls -l /root/test')     
'-rw-r--r-- 1 root root 0 Jul  1 15:34 /root/test'

作者: 清潼止水   发布时间: 2011-07-01