#!/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:
for infoField,infoValue in fileInfo:
if stat.S_ISDIR ( fileStats [ stat.ST_MODE ] ):
else:
if stat.S_ISREG( fileStats [ stat.ST_MODE ] ):
elif stat.S_ISLNK ( fileStats [ stat.ST_MODE ] ):
elif stat.S_ISSOCK ( fileStats [ stat.ST_MODE ] ):
elif stat.S_ISFIFO ( fileStats [ stat.ST_MODE ] ):
elif stat.S_ISBLK ( fileStats [ stat.ST_MODE ] ):
elif stat.S_ISCHR ( fileStats [ stat.ST_MODE ] ):
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 2033080 >>> print os.stat("/root/python/zip.py").st_dev 26626 >>> print os.stat("/root/python/zip.py").st_nlink 1 >>> print os.stat("/root/python/zip.py").st_uid 0 >>> print os.stat("/root/python/zip.py").st_gid 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 |