#!/usr/bin/env python
#-*- encoding:UTF-8 -*-

import os,time,stat

fileStats = os.stat ( 'test.txt' )                         #获取文件/目录的状态
fileInfo = {
'Size':fileStats [ stat.ST_SIZE ],                         #获取文件大小
'LastModified':time.ctime( fileStats [ stat.ST_MTIME ] ),  #获取文件最后修改时间
'LastAccessed':time.ctime( fileStats [ stat.ST_ATIME ] ),  #获取文件最后访问时间
'CreationTime':time.ctime( fileStats [ stat.ST_CTIME ] ),  #获取文件创建时间
'Mode':fileStats [ stat.ST_MODE ]                          #获取文件的模式
}
#print fileInfo

for field in fileInfo:                                     #显示对象内容
  print '%s:%s' % (field,fileInfo[field])

for infoField,infoValue in fileInfo:
  print '%s:%s' % (infoField,infoValue)
if stat.S_ISDIR ( fileStats [ stat.ST_MODE ] ):           #判断是否路径
  print 'Directory. '
else:
  print 'Non-directory.'

if stat.S_ISREG( fileStats [ stat.ST_MODE ] ):           #判断是否一般文件
   print 'Regular file.'
elif stat.S_ISLNK ( fileStats [ stat.ST_MODE ] ):         #判断是否链接文件
   print 'Shortcut.'
elif stat.S_ISSOCK ( fileStats [ stat.ST_MODE ] ):        #判断是否套接字文件    
   print 'Socket.'
elif stat.S_ISFIFO ( fileStats [ stat.ST_MODE ] ):        #判断是否命名管道
   print 'Named pipe.'
elif stat.S_ISBLK ( fileStats [ stat.ST_MODE ] ):         #判断是否块设备
   print 'Block special device.'
elif stat.S_ISCHR ( fileStats [ stat.ST_MODE ] ):         #判断是否字符设置
   print 'Character special device.'

stat模块描述了os.stat(filename)返回的文件属性列表中各值的意义.我们可方便地根据stat模块存取os.stat()中的值.
os.stat(path)执行一个stat()系统调用在给定的path上,返回一个类元组对象(stat_result对象,包含10个元素),属性与stat结构成员相关:st_mode(权限模式),st_ino(inode number),st_dev(device),st_nlink(number of hard links),st_uid(所有用户的user id),st_gid(所有用户的group id),st_size(文件大小,以位为单位),st_atime(最近访问的时间),st_mtime(最近修改的时间),st_ctime(创建的时间)

>>> 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 模块的更多相关文章

  1. python之platform模块

    python之platform模块 ^_^第三个模块从天而降喽!! 函数列表 platform.system() 获取操作系统类型,windows.linux等 platform.platform() ...

  2. python之OS模块详解

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

  3. python之sys模块详解

    python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...

  4. 学习PYTHON之路, DAY 6 - PYTHON 基础 6 (模块)

    一 安装,导入模块 安装: pip3 install 模块名称 导入: import module from module.xx.xx import xx from module.xx.xx impo ...

  5. linux下python调用c模块

    在C调用Python模块时需要初始化Python解释器,导入模块等,但Python调用C模块却比较简单,下面还是以helloWorld.c 和 main.py 做一说明:   (1)编写C代码,hel ...

  6. Python学习之模块进程函数详解

    今天在看<Beginning Linux Programming>中的进程相关部分,讲到Linux几个进程相关的系统函数: system , exec , fork ,wait . Pyt ...

  7. python基础——第三方模块

    python基础——第三方模块 在Python中,安装第三方模块,是通过包管理工具pip完成的.  如果你正在使用Mac或Linux,安装pip本身这个步骤就可以跳过了.  如果你正在使用Window ...

  8. python基础——使用模块

    python基础——使用模块 Python本身就内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用. 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env ...

  9. python 中time模块使用

    在开始之前,首先要说明这几点: 1.在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素.由于Python的time模块实现主 ...

  10. Python之logging模块

    一.引言 之前在写一些小程序的时候想把日志内容打到文件中,所以就自己写了一个logger.py的程序,如下: #!/usr/bin/python # -*- coding=utf-8 -*- impo ...

随机推荐

  1. 外星人完事了,开始python的matplotlib玩转

    外星人完事了,开始python的matplotlib玩转 看书上的例子,在win下安装比较麻烦 今天用pip尝试了一下 pip install matplotlib 然后等待即可 安装完毕后 在pyt ...

  2. ngnix配置thinkphp5隐藏index.php的方法亲测有效

    在需要访问的域名的conf文件中,比如 vim /etc/nginx/.com.conf location / { // …..省略部分代码 if (!-e $request_filename) { ...

  3. jpa-入门.缓存配置ehcache.xml

    <ehcache> <!-- Sets the path to the directory where cache .data files are created. If the p ...

  4. leetcode965

    public class Solution { List<int> list = new List<int>(); private void postTree(TreeNode ...

  5. JS吧数字转成2进制 8进制16进制数据

    ; number.toString(); //转成2进制 number.toString();//转成8进制 number.toString();//转成10进制 number.toString(); ...

  6. 如何使用Python快速制作可视化报表----pyecharts

    如何使用Python快速制作可视化报表   数据可视化能力已经越来越成为各岗位的基础技能.领英的数据报告显示,数据可视化技能在2017年中国最热门技能中排名第一. 就数据分析而言,可视化探索几乎是你正 ...

  7. TDictionary 是delphi用的,c++builder用起来太吃力。

    TDictionary 是delphi用的,c++builder用起来太吃力.c++还是用std::map代替.c++d map很好用啊.https://blog.csdn.net/ddkxddkx/ ...

  8. H5-DataUrl FileReader

    DataUrl: 将二进制文件流以字符串的形式存在,如果是图片可以在页面上展示.经常用于Canvas截图或画图展示用. 格式:data:image/png;base64,****. /** * dat ...

  9. check_http检查http服务

    选 项 说 明 -H, --hostname=ADDRESS 主机名或域名 -I, --IP-address=ADDRESS server的IP地址,用于在不能DNS的情况下 -p, --port=I ...

  10. selenium中使用chromedriver备忘

    chromedriver是chrome浏览器的webdriver的一个实现.ChromeDriver是由Chrome开发团队来完成的因而ChromeDriver不包含在selenium包中,需要从Ch ...