Python开发--stat

转载自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:

运行效果:

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

代码部分:

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

#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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值