python stat模块详解(!important)

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

 

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

 

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.'

 

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)
>>> stat.S_IMODE(mode)
>>> print oct(stat.S_IMODE(mode))#oct 是转换为八进制

 

posted @ 2017-05-18 14:49  Adamanter  阅读(595)  评论(0编辑  收藏  举报