stat 系统调用时用来返回相关文件的系统状态信息的。

首先我们看一下stat中有哪些属性:

>>> import os
>>> print os.stat("/root/python/zip.py")
(33188, 2033080, 26626L, 1, 0, 0, 864, 1297653596, 1275528102, 1292892895)
>>> print os.stat("/root/python/zip.py").st_mode #权限模式
33188
>>> print os.stat("/root/python/zip.py").st_ino #inode number
2033080
>>> print os.stat("/root/python/zip.py").st_dev #device
26626
>>> print os.stat("/root/python/zip.py").st_nlink #number of hard links
1
>>> print os.stat("/root/python/zip.py").st_uid #所有用户的user id
0
>>> print os.stat("/root/python/zip.py").st_gid #所有用户的group id
0
>>> print os.stat("/root/python/zip.py").st_size #文件的大小,以位为单位
864
>>> print os.stat("/root/python/zip.py").st_atime #文件最后访问时间
1297653596
>>> print os.stat("/root/python/zip.py").st_mtime #文件最后修改时间
1275528102
>>> print os.stat("/root/python/zip.py").st_ctime #文件创建时间
1292892895

正如你上面看到的,你可以直接访问到这些属性值。

好了,下面我来看看python中的stat模块,先看看自带的例子:

import os, sys
from stat import * def walktree(top, callback):
'''recursively descend the directory tree rooted at top,
calling the callback function for each regular file''' for f in os.listdir(top):
pathname = os.path.join(top, f)
mode = os.stat(pathname).st_mode
if S_ISDIR(mode):
# It's a directory, recurse into it
walktree(pathname, callback)
elif S_ISREG(mode):
# It's a file, call the callback function
callback(pathname)
else:
# Unknown file type, print a message
print 'Skipping %s' % pathname def visitfile(file):
print 'visiting', file if __name__ == '__main__':
walktree(sys.argv[], visitfile)

可以这么理解,os.stat是将文件的相关属性读出来,然后用stat模块来处理,处理方式有多重,就要看看stat提供了什么了。

1.  可以对st_mode做相关的判断,如是否是目录,是否是文件,是否是管道等。

先看一下处理os.stat返回的st_mode结果的函数,就想上面的例子中的一样,这些函数可以做出判断:

if stat.S_ISREG(mode):           #判断是否一般文件
print 'Regular file.'
elif stat.S_ISLNK (mode): #判断是否链接文件
print 'Shortcut.'
elif stat.S_ISSOCK (mode): #判断是否套接字文件
print 'Socket.'
elif stat.S_ISFIFO (mode): #判断是否命名管道
print 'Named pipe.'
elif stat.S_ISBLK (mode): #判断是否块设备
print 'Block special device.'
elif stat.S_ISCHR (mode): #判断是否字符设置
  print 'Character special device.'
elif stat.S_ISDIR (mode):         #判断是否目录
  print 'directory.'
##额外的两个函数
stat.S_IMODE (mode): #返回文件权限的chmod格式
  print 'chmod format.'

stat.S_IFMT (mode): #返回文件的类型
  print 'type of fiel.'
 

2.   还有一些是各种各样的标示符,这些标示符也可以在os.chmod中使用,下面附上这些标示符的说明:

    stat.S_ISUID: Set user ID on execution.                      不常用

    stat.S_ISGID: Set group ID on execution.                    不常用

    stat.S_ENFMT: Record locking enforced.                                          不常用

    stat.S_ISVTX: Save text image after execution.                                在执行之后保存文字和图片

    stat.S_IREAD: Read by owner.                                                           对于拥有者读的权限

    stat.S_IWRITE: Write by owner.                                                         对于拥有者写的权限

    stat.S_IEXEC: Execute by owner.                                                       对于拥有者执行的权限

    stat.S_IRWXU: Read, write, and execute by owner.                          对于拥有者读写执行的权限

    stat.S_IRUSR: Read by owner.                                                            对于拥有者读的权限

    stat.S_IWUSR: Write by owner.                                                          对于拥有者写的权限

    stat.S_IXUSR: Execute by owner.                                                       对于拥有者执行的权限

    stat.S_IRWXG: Read, write, and execute by group.                                 对于同组的人读写执行的权限

    stat.S_IRGRP: Read by group.                                                             对于同组读的权限

    stat.S_IWGRP: Write by group.                                                           对于同组写的权限

    stat.S_IXGRP: Execute by group.                                                        对于同组执行的权限

    stat.S_IRWXO: Read, write, and execute by others.                          对于其他组读写执行的权限

    stat.S_IROTH: Read by others.                                                           对于其他组读的权限

    stat.S_IWOTH: Write by others.                                                         对于其他组写的权限

    stat.S_IXOTH: Execute by others.                                                      对于其他组执行的权限

例子:我想获得某个文件的属性信息,并查看他的权限信息,用chmod的格式显示出来。

>>> import stat
>>> import os
>>> st = os.stat('sig.txt')
>>> mode = st.st_mode
>>> stat.S_IFMT(mode)
32768
>>> stat.S_IMODE(mode)
438
>>> print oct(stat.S_IMODE(mode))#oct 是转换为八进制
0666

python os.stat() 和 stat模块详解的更多相关文章

  1. (转)python标准库中socket模块详解

    python标准库中socket模块详解 socket模块简介 原文:http://www.lybbn.cn/data/datas.php?yw=71 网络上的两个程序通过一个双向的通信连接实现数据的 ...

  2. Python之import方法引入模块详解

    在python用import或者from-import或者from-import-as-来导入相应的模块,作用和使用方法与C语言的include头文件类似.其实就是引入某些成熟的函数库和成熟的方法,避 ...

  3. python与正则表达式:re模块详解

    re模块是python中处理正在表达式的一个模块 正则表达式知识储备:http://www.cnblogs.com/huamingao/p/6031411.html 1. match(pattern, ...

  4. python的argpare和click模块详解

    一.argparse模块 1.模块说明 # argparse是python的标准库中用来解析命令行参数的模块,用来替代已经过时的optparse模块,argparse能够根据程序中的定义的sys.ar ...

  5. Python基础系列讲解——时间模块详解大全之time模块

    Python中提供处理时间日期相关的内置模块有time.datetime和calendar. time模块中大多数函数调用了所在平台C library 的同名函数,因此更依赖于操作系统层面,所以tim ...

  6. Python运维自动化psutil 模块详解(超级详细)

    psutil 模块 参考官方文档:https://pypi.org/project/psutil/ 一.psutil简介 psutil是一个开源且跨平台(http://code.google.com/ ...

  7. python标准库中socket模块详解

    包含原理就是tcp的三次握手 http://www.lybbn.cn/data/datas.php?yw=71 这篇讲到了socket和django的联系 https://www.cnblogs.co ...

  8. python之OS模块详解

    python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...

  9. (转)python之os,sys模块详解

    python之sys模块详解 原文:http://www.cnblogs.com/cherishry/p/5725184.html sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和 ...

随机推荐

  1. laravel框架少见方法详解

    1.whereDate() 方法 $q->where('created_at', '>=', date('Y-m-d').' 00:00:00')); 以前查数据时,直接用where条件来 ...

  2. PHP 5.3.X 连接MS SQL Server php_mssql.dll

    在网上搜索了一下PHP 5.3.X 连接SQL Server的办法,有人也遇到了这个问题 原来PHP 团队在PHP 5.3 中移除了SQL Server的驱动和库,而微软自己开发了针对PHP的SQL驱 ...

  3. 重绘panel控件,实现panel的阴影效果

    最近想在项目中添加一个要有阴影的panel控件,找了好多资料,最后通过采用图片的方式实现了panel的阴影效果,效果图如下: 重绘代码如下: using System; using System.Co ...

  4. python学习2——数据类型

    1. python是强类型 动态类型的语言,动态类型表明它可以在声明变量的时候,不必指定数据类型,强类型规定了它不能容忍隐式类型转换 2. python中的不可变类型有:int,string,tupl ...

  5. WPF中的瀑布流布局(TilePanel)控件

    最近在用wpf做一个metro风格的程序,需要用到win8风格的布局容器,只能自己写一个了.效果如下 用法 : <local:TilePanel                          ...

  6. 括弧匹配检验(check)

    /*题目:括弧匹配检验 检验给定表达式中括弧是否正确匹配 (两种括弧“( ) ”“[]" ,正确输出OK,错误则输出wrong. 2016年8月8日07:24:58 作者:冰樱梦 */ # ...

  7. c/c++常用代码--清空目录

    #pragma once #include <io.h>#include <stdio.h>#include <string>#include <direct ...

  8. matlab实现高斯消去法、LU分解

    朴素高斯消去法: function x = GauElim(n, A, b) if nargin < 2 for i = 1 : 1 : n for j = 1 : 1 : n A(i, j) ...

  9. 构件图 Component Diagram

    构件图是显示代码自身结构的实现级别的图表.构件图由诸如源代码文件.二进制代码文件.可执行文件或动态链接库 (DLL) 这样的构件构成,并通过依赖关系相连接 下面这张图介绍了构件图的基本内容: 下面这张 ...

  10. Daily Scrum 11.6

    摘要:在本次meeting时,所有代码的修改工作已经接近尾声,接下来是进行的就是单元测试以及进行alpha版本的改进.本次的Task列表如下: Task列表 出席人员 Today's Task Tom ...