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控制台英汉汉英电子词典
Apr 23 Python
Python multiprocessing模块中的Pipe管道使用实例
Apr 11 Python
Django restframework 源码分析之认证详解
Feb 22 Python
把JSON数据格式转换为Python的类对象方法详解(两种方法)
Jun 04 Python
python批量修改ssh密码的实现
Aug 08 Python
Python paramiko模块使用解析(实现ssh)
Aug 30 Python
python 动态迁移solr数据过程解析
Sep 04 Python
Django框架中间件定义与使用方法案例分析
Nov 28 Python
Django自定义全局403、404、500错误页面的示例代码
Mar 08 Python
python构造IP报文实例
May 05 Python
Python爬虫实现HTTP网络请求多种实现方式
Jun 19 Python
浅析关于Keras的安装(pycharm)和初步理解
Oct 23 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
在“咖啡之国”感受咖啡文化
2021/03/03 咖啡文化
php获取post中的json数据的实现方法
2011/06/08 PHP
探讨:php中在foreach中使用foreach ($arr as &$value) 这种类型的解释
2013/06/24 PHP
php中数字0和空值的区别分析
2014/06/05 PHP
thinkphp备份数据库的方法分享
2015/01/04 PHP
10个超级有用值得收藏的PHP代码片段
2015/01/22 PHP
PHP SPL标准库之数据结构栈(SplStack)介绍
2015/05/12 PHP
PHP使用pear实现mail发送功能 windows环境下配置pear
2016/04/15 PHP
javascript multibox 全选
2009/03/22 Javascript
Javascript 命名空间模式
2013/11/01 Javascript
js光标定位文本框回车表单提交问题的解决方法
2015/05/11 Javascript
jQuery动态生成表格及右键菜单功能示例
2017/01/13 Javascript
Boostrap栅格系统与自己额外定义的媒体查询的冲突问题
2017/02/19 Javascript
微信小程序--onShareAppMessage分享参数用处(页面分享)
2017/04/18 Javascript
Javascript中八种遍历方法的执行速度深度对比
2017/04/25 Javascript
微信小程序动态添加分享数据
2017/06/14 Javascript
jQuery动画_动力节点节点Java学院整理
2017/07/04 jQuery
JavaScript实现的仿新浪微博原生态输入字数即时检查功能【兼容IE6】
2017/09/26 Javascript
vuex实现登录状态的存储,未登录状态不允许浏览的方法
2018/03/09 Javascript
Vue.js中 v-model 指令的修饰符详解
2018/12/03 Javascript
JQuery实现ul中添加LI和删除指定的Li元素功能完整示例
2019/10/16 jQuery
p5.js绘制创意自画像
2019/11/04 Javascript
Vue filter 过滤当前时间 实现实时更新效果
2019/12/20 Javascript
idea创建springMVC框架和配置小文件的教程图解
2018/09/18 Python
python3中的eval和exec的区别与联系
2019/10/10 Python
Python多重继承之菱形继承的实例详解
2020/02/12 Python
详解canvas多边形(蜘蛛图)的画法示例
2018/01/29 HTML / CSS
潘多拉珠宝美国官方网站:Pandora US
2020/06/18 全球购物
初中班主任评语大全
2014/04/24 职场文书
2014年度安全生产目标管理责任书
2014/07/25 职场文书
干部作风整顿自我剖析材料和整改措施
2014/09/18 职场文书
未受刑事制裁公证证明
2014/09/20 职场文书
2014年扶贫帮困工作总结
2014/12/09 职场文书
个人事迹材料范文
2014/12/29 职场文书
2015年语文教学工作总结
2015/05/25 职场文书
FP-growth算法发现频繁项集——构建FP树
2021/06/24 Python