python开发_stat
发布日期:2021-06-24 18:15:21 浏览次数:4 分类:技术文章

本文共 3639 字,大约阅读时间需要 12 分钟。

当我们使用获取一个文件(夹)信息的时候,

本身返回的是一个元组如:
     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:

运行效果:

==============================================

代码部分:

==============================================

1 #python stat 2 ''' 3     当我们使用os.stat(path)获取一个文件(夹)信息的时候, 4     os.stat(path)本身返回的是一个元组如: 5      6     nt.stat_result(st_mode=33206, st_ino=203224933185146561, st_dev=0, 7     st_nlink=1, st_uid=0, st_gid=0, st_size=21090, st_atime=1376373336, 8     st_mtime=1376534141, st_ctime=1376373336) 9 10     在这个元组中,包含了10个属性:11     st_mode    -- protection bits(模式)12     st_ino     -- inode number(索引号)13     st_dev     -- device(设备)14     st_nlink   -- number of hard links(硬链接号)15     st_uid     -- user id of owner(用户id)16     st_gid     -- group id of owner (组id)17     st_size    -- size of file,in bytes (大小)18     st_atime   -- time of most recent access expressed in seconds (访问时间)19     st_mtime   -- time of most recent content modificatin expressed in seconds (修改时间)20     st_ctime   -- platform dependent;time of most recent metadata change on Unix,21                   or the teime of creation on Windows,expressed in senconds (根据不同操作系统而定)22 23     #############################################################24     而stat在这里起到什么作用呢?25     类似于java中定义的一些常量:26     如:27         os.stat(path).st_size28         os.stat(path)[stat.ST_SIZE]29         这两种表示方法是一样的。30 '''31 import os32 import time33 import stat34 35 def get_file_stat(path):36     '''获取一个文件(夹)信息,该信息将以一个元组的形式返回'''37     if os.path.exists(path):38         return os.stat(path)39     else:40         print('the path [{}] is not exist!'.format(path))41 42 def print_info(file_stat):43     '''打印信息'''44     if file_stat != None:45         file_info = {46             'Size' : file_stat [ stat.ST_SIZE ],                         #获取文件大小47             'LastModified' : time.ctime( file_stat [ stat.ST_MTIME ] ),  #获取文件最后修改时间48             'LastAccessed' : time.ctime( file_stat [ stat.ST_ATIME ] ),  #获取文件最后访问时间49             'CreationTime' : time.ctime( file_stat [ stat.ST_CTIME ] ),  #获取文件创建时间50             'Mode' : file_stat [ stat.ST_MODE ],                         #获取文件的模式51             'Device' : file_stat [stat.ST_DEV],                          #设备52             'UserID' : file_stat [stat.ST_UID],53             'GroupID' : file_stat [stat.ST_GID]54             }55         for key in file_info:56             print('{} : {}'.format(key, file_info[key]))57     else:58         print('the stat is None!')59 60 def main():61     path_dir = 'c:\\Download'62     path_file = 'c:\\test.html'63     print('目录信息:')64     file_stat = get_file_stat(path_dir)65     print_info(file_stat)66     print('#' * 50)67     print('文件信息:')68     file_stat = get_file_stat(path_file)69     print_info(file_stat)70 71 if __name__ == '__main__':72     main()73

 

转载地址:https://blog.csdn.net/weixin_34248258/article/details/86190067 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:图像缩放的双线性内插值算法的原理解析
下一篇:更改SQL实例端口

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月20日 15时54分05秒