Python's Stat module

Source: Internet
Author: User

#!/usr/bin/env python
#-*-Encoding:utf-8-*-

Import Os,time,stat

Filestats = Os.stat (' test.txt ')                            #获取文件/Directory status
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. '

IfStat. S_isreg(Filestats [Stat. St_mode]): #判断是否一般文件
print ' Regular file. '
ElifStat. S_islnk(Filestats [Stat. St_mode]): #判断是否链接文件
print ' Shortcut. '
ElifStat. S_issock(Filestats [Stat. St_mode]): #判断是否套接字文件
print ' Socket. '
ElifStat. S_isfifo(Filestats [Stat. St_mode]): #判断是否命名管道
print ' Named pipe. '
ElifStat. S_isblk(Filestats [Stat. St_mode]): #判断是否块设备
print ' Block special device. '
ElifStat. S_ISCHR(Filestats [Stat. St_mode]): #判断是否字符设置
print ' Character special device. '

The stat module describes the meaning of the values in the list of file attributes returned by Os.stat (filename). We can easily access the values in Os.stat () based on the stat module.
Os.stat (PATH) executes a stat () system call that returns a class tuple object (Stat_result object, containing 10 elements) on the given path, with attributes associated with the stat struct member:st_mode(permission mode), St_ino (inode number),St_dev(device),St_nlink(number of hard links),St_uid(user ID for all users),St _gid(group ID for all users),st_size(file size, in bits),st_atime(last accessed time),st_mtime(last modified time), St_ctime (Time of Creation)

>>> 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 #文件的大小, in bits
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's Stat module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.