Python3 获取文件属性的方式(时间、大小等)


Posted in Python onMarch 12, 2020

os.stat(path) :

用于在给定的路径上执行一个系统 stat 的调用。

path:

指定路径

返回值:

st_mode: inode 保护模式
-File mode: file type and file mode bits (permissions).
st_ino: inode 节点号。
-Platform dependent, but if non-zero, uniquely identifies the file for a given value of st_dev.
——the inode number on Unix,
——the file index on Windows
st_dev: inode 驻留的设备。
-Identifier of the device on which this file resides.
st_nlink:inode 的链接数。
-Number of hard links.
st_uid: 所有者的用户ID。
-User identifier of the file owner.
st_gid: 所有者的组ID。
-Group identifier of the file owner.
st_size:普通文件以字节为单位的大小;包含等待某些特殊文件的数据。
-Size of the file in bytes, if it is a regular file or a symbolic link. The size of a symbolic link is the length of the pathname it contains, without a terminating null byte.
st_atime: 上次访问的时间。
-Time of most recent access expressed in seconds.
st_mtime: 最后一次修改的时间。
-Time of most recent content modification expressed in seconds.
st_ctime:由操作系统报告的"ctime"。在某些系统上(如Unix)是最新的元数据更改的时间,在其它系统上(如Windows)是创建时间(详细信息参见平台的文档)。
st_atime_ns
-Time of most recent access expressed in nanoseconds as an integer
st_mtime_ns
-Time of most recent content modification expressed in nanoseconds as an integer.
st_ctime_ns
-Platform dependent:
——the time of most recent metadata change on Unix,
——the time of creation on Windows, expressed in nanoseconds as an integer.

实例:

from os import stat
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

from os import stat
import time
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)

附:月份缩写 -_-||

Python3 获取文件属性的方式(时间、大小等)

time 模块的 strftime 方法来格式化日期

print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(statinfo.st_atime)))

结果:

2018-11-01 08:36:03

附:格式化符号

%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(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链接提取下载文件的大小特征和下载文件类型
def getRemoteFileSize(url, proxy=None):
  '''
  通过content-length头获取远程文件大小
  '''
  opener = urllib2.build_opener()
  if proxy:
    if url.lower().startswith('https://'):
      opener.add_handler(urllib2.ProxyHandler({'https' : proxy}))
    elif url.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()
  except Exception, e:
    # 远程文件不存在
    return 0, 0
  else:
    getfileSize = dict(response.headers).get('content-length', 0)
    filesize = round(float(getfileSize) / 1048576, 2)
    getContentType = dict(response.headers).get('content-type', 0)
    return filesize, getContentType

以上这篇Python3 获取文件属性的方式(时间、大小等)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python中的内置函数getattr()介绍及示例
Jul 20 Python
对于Python异常处理慎用“except:pass”建议
Apr 02 Python
python字符串对其居中显示的方法
Jul 11 Python
Python实现将SQLite中的数据直接输出为CVS的方法示例
Jul 13 Python
全面分析Python的优点和缺点
Feb 07 Python
对python抓取需要登录网站数据的方法详解
May 21 Python
在python中对变量判断是否为None的三种方法总结
Jan 23 Python
浅谈Python3 numpy.ptp()最大值与最小值的差
Aug 24 Python
Python ATM功能实现代码实例
Mar 19 Python
深入了解Python 变量作用域
Jul 24 Python
浅析Python 责任链设计模式
Sep 11 Python
Python 图片处理库exifread详解
Feb 25 Python
Python获取对象属性的几种方式小结
Mar 12 #Python
深入浅析Python 命令行模块 Click
Mar 11 #Python
python字典和json.dumps()的遇到的坑分析
Mar 11 #Python
解决pyecharts运行后产生的html文件用浏览器打开空白
Mar 11 #Python
在django admin详情表单显示中添加自定义控件的实现
Mar 11 #Python
django admin 添加自定义链接方式
Mar 11 #Python
django xadmin 管理器常用显示设置方式
Mar 11 #Python
You might like
Windows下XDebug 手工配置与使用说明
2010/07/11 PHP
PHP投票系统防刷票判断流程分析
2012/02/04 PHP
php模板函数 正则实现代码
2012/10/15 PHP
使用PHP备份MYSQL数据的多种方法
2014/01/15 PHP
baidu博客的编辑友情链接的新的层窗口!经典~支持【FF】
2007/02/09 Javascript
兼容多浏览器的iframe自适应高度(ie8 、谷歌浏览器4.0和 firefox3.5.3)
2009/11/04 Javascript
javascript 获取表单file全路径
2009/12/31 Javascript
js 获取计算后的样式写法及注意事项
2013/02/25 Javascript
js获取当前地址 JS获取当前URL的示例代码
2014/02/26 Javascript
微信JS接口汇总及使用详解
2015/01/09 Javascript
javascript自动生成包含数字与字符的随机字符串
2015/02/09 Javascript
js中遍历Map对象的简单实例
2016/08/08 Javascript
node.js Sequelize实现单实例字段或批量自增、自减
2016/12/08 Javascript
vue todo-list组件发布到npm上的方法
2018/04/04 Javascript
详解html-webpack-plugin插件(用法总结)
2018/09/12 Javascript
JavaScript中Dom操作实例详解
2019/07/08 Javascript
基于layui的下拉列表的数据回显方法
2019/09/24 Javascript
Vue项目打包压缩的实现(让页面更快响应)
2020/03/10 Javascript
[01:05:56]2018DOTA2亚洲邀请赛3月29日 小组赛A组 Newbee VS VG
2018/03/30 DOTA
详解Django中Request对象的相关用法
2015/07/17 Python
深入解析Python中的变量和赋值运算符
2015/10/12 Python
Python 实现引用其他.py文件中的类和类的方法
2018/04/29 Python
python 爬虫一键爬取 淘宝天猫宝贝页面主图颜色图和详情图的教程
2018/05/22 Python
Python空间数据处理之GDAL读写遥感图像
2019/08/01 Python
Python 70行代码实现简单算式计算器解析
2019/08/30 Python
Python3加密解密库Crypto的RSA加解密和签名/验签实现方法实例
2020/02/11 Python
CSS3 box-sizing属性
2009/04/17 HTML / CSS
HTML5之SVG 2D入门10—滤镜的定义及使用
2013/01/30 HTML / CSS
美国瑜伽服装和装备购物网站:Mukha Yoga
2019/02/22 全球购物
95%的面试官都会问到的50道Java线程题,附答案
2012/08/03 面试题
幼儿园元旦活动感言
2014/03/02 职场文书
社区工作者演讲稿
2014/05/23 职场文书
国庆节演讲稿范文2014
2014/09/19 职场文书
节约用电倡议书
2015/04/28 职场文书
2015年小学体育教师工作总结
2015/10/23 职场文书
股东合作协议书模板2篇
2019/11/05 职场文书