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的Tornado框架实现图片上传及图片大小修改功能
Jun 30 Python
Python安装Numpy和matplotlib的方法(推荐)
Nov 02 Python
tensorflow 获取变量&打印权值的实例讲解
Jun 14 Python
浅谈python中拼接路径os.path.join斜杠的问题
Oct 23 Python
python实现网页自动签到功能
Jan 21 Python
使用python接入微信聊天机器人
Mar 31 Python
python打包成so文件过程解析
Sep 28 Python
Python绘制三角函数图(sin\cos\tan)并标注特定范围的例子
Dec 04 Python
在django中使用post方法时,需要增加csrftoken的例子
Mar 13 Python
利用4行Python代码监测每一行程序的运行时间和空间消耗
Apr 22 Python
使用Keras实现Tensor的相乘和相加代码
Jun 18 Python
Python用来做Web开发的优势有哪些
Aug 05 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
把77A收信机改造成收音机
2021/03/02 无线电
十天学会php之第三天
2006/10/09 PHP
PHP5 的对象赋值机制介绍
2011/08/02 PHP
PHP函数eval()介绍和使用示例
2014/08/20 PHP
教你识别简单的免查杀PHP后门
2015/09/13 PHP
JSON 客户端和服务器端的格式转换
2009/08/27 Javascript
JavaScript中OnLoad几种使用方法
2012/12/15 Javascript
jQuery 中国省市两级联动选择附图
2014/05/14 Javascript
使用Jquery实现每日签到功能
2015/04/03 Javascript
JavaScript生成福利彩票双色球号码
2015/05/15 Javascript
js关于getImageData跨域问题的解决方法
2016/10/14 Javascript
Bootstrap简单表单显示学习笔记
2016/11/15 Javascript
jQuery对table表格进行增删改查
2020/12/22 Javascript
实例教学如何写vue插件
2017/11/30 Javascript
ES6与CommonJS中的模块处理的区别
2018/06/13 Javascript
vue 监听某个div垂直滚动条下拉到底部的方法
2018/09/15 Javascript
vue头部导航动态点击处理方法
2018/11/02 Javascript
vue 中 命名视图的用法实例详解
2019/08/14 Javascript
JavaScript缓动动画函数的封装方法
2020/11/25 Javascript
[00:34]TI7不朽珍藏III——地穴编织者不朽展示
2017/07/15 DOTA
简单上手Python中装饰器的使用
2015/07/12 Python
Python操作Excel之xlsx文件
2017/03/24 Python
python 计算两个日期相差多少个月实例代码
2017/05/24 Python
利用python实现简单的邮件发送客户端示例
2017/12/23 Python
pandas dataframe的合并实现(append, merge, concat)
2019/06/24 Python
PyTorch的深度学习入门教程之构建神经网络
2019/06/27 Python
python+selenium 鼠标事件操作方法
2019/08/24 Python
html5+CSS3+JS实现七夕言情功能代码
2017/08/28 HTML / CSS
Linux文件系统类型
2012/02/15 面试题
退学证明范本3篇
2014/10/29 职场文书
2014年党支部书记工作总结
2014/12/04 职场文书
董存瑞观后感
2015/06/11 职场文书
公司中层管理培训心得体会
2016/01/11 职场文书
解决python存数据库速度太慢的问题
2021/04/23 Python
Mysql 如何实现多张无关联表查询数据并分页
2021/06/05 MySQL
引用计数法和root搜索算法以及JVM中判定对象需要回收的方法
2022/04/19 Java/Android