Python3 获取文件属性的方式(时间、大小等)
os.stat(path):
用于在给定的路径上执行一个系统stat的调用。
path:
指定路径
返回值:
st_mode:inode保护模式
-Filemode:filetypeandfilemodebits(permissions).
st_ino:inode节点号。
-Platformdependent,butifnon-zero,uniquelyidentifiesthefileforagivenvalueofst_dev.
——theinodenumberonUnix,
——thefileindexonWindows
st_dev:inode驻留的设备。
-Identifierofthedeviceonwhichthisfileresides.
st_nlink:inode的链接数。
-Numberofhardlinks.
st_uid:所有者的用户ID。
-Useridentifierofthefileowner.
st_gid:所有者的组ID。
-Groupidentifierofthefileowner.
st_size:普通文件以字节为单位的大小;包含等待某些特殊文件的数据。
-Sizeofthefileinbytes,ifitisaregularfileorasymboliclink.Thesizeofasymboliclinkisthelengthofthepathnameitcontains,withoutaterminatingnullbyte.
st_atime:上次访问的时间。
-Timeofmostrecentaccessexpressedinseconds.
st_mtime:最后一次修改的时间。
-Timeofmostrecentcontentmodificationexpressedinseconds.
st_ctime:由操作系统报告的"ctime"。在某些系统上(如Unix)是最新的元数据更改的时间,在其它系统上(如Windows)是创建时间(详细信息参见平台的文档)。
st_atime_ns
-Timeofmostrecentaccessexpressedinnanosecondsasaninteger
st_mtime_ns
-Timeofmostrecentcontentmodificationexpressedinnanosecondsasaninteger.
st_ctime_ns
-Platformdependent:
——thetimeofmostrecentmetadatachangeonUnix,
——thetimeofcreationonWindows,expressedinnanosecondsasaninteger.
实例:
fromosimportstat statinfo=stat(r'C:\Users\Administrator\Desktop\1\4D-A300.txt') print(statinfo)#属性 print(statinfo.st_size)#大小字节 print('%.3f'%(statinfo.st_size/1024/1024))#大小M
输出结果:
os.stat_result(st_mode=33206,st_ino=3659174697378650,st_dev=3993776408,st_nlink=1,st_uid=0,st_gid=0,st_size=3876301,st_atime=1541032563,st_mtime=1541033475,st_ctime=1541032563) .697
我们看到,时间都是一些大的浮点数-时间戳(每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表示。)
从返回浮点数的时间辍方式向时间元组转换,只要将浮点数传递给如localtime之类的函数。
#-*-coding:utf-8-*-python3.6.3 fromosimportstat importtime statinfo=stat(r'C:\Users\Administrator\Desktop\1\4D-A300.txt') print(statinfo) print(time.localtime(statinfo.st_atime))
输出为:
os.stat_result(st_mode=33206,st_ino=3659174697378650,st_dev=3993776408,st_nlink=1,st_uid=0,st_gid=0,st_size=3876301,st_atime=1541032563,st_mtime=1541033475,st_ctime=1541032563) time.struct_time(tm_year=2018,tm_mon=11,tm_mday=1,tm_hour=8,tm_min=36,tm_sec=3,tm_wday=3,tm_yday=305,tm_isdst=0)
附:月份缩写-_-||
time模块的strftime方法来格式化日期
print(time.strftime("%Y-%m-%d%H:%M:%S",time.localtime(statinfo.st_atime)))
结果:
2018-11-0108:36:03
附:格式化符号
%y两位数的年份表示(00-99)
%Y四位数的年份表示(000-9999)
%m月份(01-12)
%d月内中的一天(0-31)
%H24小时制小时数(0-23)
%I12小时制小时数(01-12)
%M分钟数(00=59)
%S秒(00-59)
%a本地简化星期名称
%A本地完整星期名称
%b本地简化的月份名称
%B本地完整的月份名称
%c本地相应的日期表示和时间表示
%j年内的一天(001-366)
%p本地A.M.或P.M.的等价符
%U一年中的星期数(00-53)星期天为星期的开始
%w星期(0-6),星期天为星期的开始
%W一年中的星期数(00-53)星期一为星期的开始
%x本地相应的日期表示
%X本地相应的时间表示
%Z当前时区的名称
%%%号本身
补充知识:python获取请求链接下载文件的大小和文件特征
废话不多说,还只直接看代码吧!
###根据url链接提取下载文件的大小特征和下载文件类型 defgetRemoteFileSize(url,proxy=None): ''' 通过content-length头获取远程文件大小 ''' opener=urllib2.build_opener() ifproxy: ifurl.lower().startswith('https://'): opener.add_handler(urllib2.ProxyHandler({'https':proxy})) elifurl.lower().startswith('http://'): opener.add_handler(urllib2.ProxyHandler({'http':proxy})) else: opener.add_handler(urllib2.ProxyHandler({'ftp':proxy})) try: request=urllib2.Request(url) request.get_method=lambda:'HEAD' response=opener.open(request) response.read() exceptException,e: #远程文件不存在 return0,0 else: getfileSize=dict(response.headers).get('content-length',0) filesize=round(float(getfileSize)/1048576,2) getContentType=dict(response.headers).get('content-type',0) returnfilesize,getContentType
以上这篇Python3获取文件属性的方式(时间、大小等)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。