转载自http://www.cnblogs.com/hongten/p/hongten_python_stat.html
当我们使用os.stat(path)获取一个文件(夹)信息的时候,
os.stat(path)本身返回的是一个元组如:
nt.stat_result(st_mode=33206, st_ino=203224933185146561, st_dev=0,
st_nlink=1, st_uid=0, st_gid=0, st_size=21090, st_atime=1376373336,
st_mtime=1376534141, st_ctime=1376373336)
在这个元组中,包含了10个属性:
st_mode -- protection bits(模式)
st_ino -- inode number(索引号)
st_dev -- device(设备)
st_nlink -- number of hard links(硬链接号)
st_uid -- user id of owner(用户id)
st_gid -- group id of owner (组id)
st_size -- size of file,in bytes (大小)
st_atime -- time of most recent access expressed in seconds (访问时间)
st_mtime -- time of most recent content modificatin expressed in seconds (修改时间)
st_ctime -- platform dependent;time of most recent metadata change on Unix,
or the teime of creation on Windows,expressed in senconds (根据不同操作系统而定)
#############################################################
而stat在这里起到什么作用呢?
类似于java中定义的一些常量:
如:
os.stat(path).st_size
os.stat(path)[stat.ST_SIZE]
这两种表示方法是一样的。
下面是我做的demo:
运行效果:
![](https://i-blog.csdnimg.cn/blog_migrate/46fde2e0f0c7e0157fc67304b6545378.png)
==============================================
代码部分:
==============================================
#python stat'''
当我们使用os.stat(path)获取一个文件(夹)信息的时候,
os.stat(path)本身返回的是一个元组如:
nt.stat_result(st_mode=33206, st_ino=203224933185146561, st_dev=0,
st_nlink=1, st_uid=0, st_gid=0, st_size=21090, st_atime=1376373336,
st_mtime=1376534141, st_ctime=1376373336)
在这个元组中,包含了10个属性:
st_mode -- protection bits(模式)
st_ino -- inode number(索引号)
st_dev -- device(设备)
st_nlink -- number of hard links(硬链接号)
st_uid -- user id of owner(用户id)
st_gid -- group id of owner (组id)
st_size -- size of file,in bytes (大小)
st_atime -- time of most recent access expressed in seconds (访问时间)
st_mtime -- time of most recent content modificatin expressed in seconds (修改时间)
st_ctime -- platform dependent;time of most recent metadata change on Unix,
or the teime of creation on Windows,expressed in senconds (根据不同操作系统而定)
#############################################################
而stat在这里起到什么作用呢?
类似于java中定义的一些常量:
如:
os.stat(path).st_size
os.stat(path)[stat.ST_SIZE]
这两种表示方法是一样的。
'''
import os
import time
import stat
def get_file_stat(path):
'''获取一个文件(夹)信息,该信息将以一个元组的形式返回'''
if os.path.exists(path):
return os.stat(path)
else:
print('the path [{}] is not exist!'.format(path))
def print_info(file_stat):
'''打印信息'''
if file_stat != None:
file_info = {
'Size' : file_stat [ stat.ST_SIZE ], #获取文件大小
'LastModified' : time.ctime( file_stat [ stat.ST_MTIME ] ), #获取文件最后修改时间
'LastAccessed' : time.ctime( file_stat [ stat.ST_ATIME ] ), #获取文件最后访问时间
'CreationTime' : time.ctime( file_stat [ stat.ST_CTIME ] ), #获取文件创建时间
'Mode' : file_stat [ stat.ST_MODE ], #获取文件的模式
'Device' : file_stat [stat.ST_DEV], #设备
'UserID' : file_stat [stat.ST_UID],
'GroupID' : file_stat [stat.ST_GID]
}
for key in file_info:
print('{} : {}'.format(key, file_info[key]))
else:
print('the stat is None!')
def main():
path_dir = 'c:\\Download'
path_file = 'c:\\test.html'
print('目录信息:')
file_stat = get_file_stat(path_dir)
print_info(file_stat)
print('#' * 50)
print('文件信息:')
file_stat = get_file_stat(path_file)
print_info(file_stat)
if __name__ == '__main__':
main()