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求最大值,不使用内置函数的实现方法
Jul 09 Python
djano一对一、多对多、分页实例代码
Aug 16 Python
python 3.74 运行import numpy as np 报错lib\site-packages\numpy\__init__.py
Oct 06 Python
python判断变量是否为int、字符串、列表、元组、字典的方法详解
Feb 13 Python
python logging.basicConfig不生效的原因及解决
Feb 20 Python
pandas数据选取:df[] df.loc[] df.iloc[] df.ix[] df.at[] df.iat[]
Apr 24 Python
Python3自定义http/https请求拦截mitmproxy脚本实例
May 11 Python
Spring http服务远程调用实现过程解析
Jun 11 Python
Keras 利用sklearn的ROC-AUC建立评价函数详解
Jun 15 Python
Python使用文件操作实现一个XX信息管理系统的示例
Jul 02 Python
Pycharm自带Git实现版本管理的方法步骤
Sep 18 Python
python多线程爬取西刺代理的示例代码
Jan 30 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/04 冲泡冲煮
编译问题
2006/10/09 PHP
PHP字符串处理的10个简单方法
2010/06/30 PHP
逆序二维数组插入一元素的php代码
2012/06/08 PHP
解析PHP工厂模式的好处
2013/06/18 PHP
php后门URL的防范
2013/11/12 PHP
Kindeditor编辑器添加图片上传水印功能(php代码)
2017/08/03 PHP
PHP模版引擎原理、定义与用法实例
2019/03/29 PHP
JAVASCRIPT车架号识别/验证函数代码 汽车车架号验证程序
2012/01/08 Javascript
Javascript面向对象扩展库代码分享
2012/03/27 Javascript
Extjs改变树节点的勾选状态点击按钮将复选框去掉
2013/11/14 Javascript
Javascript中匿名函数的多种调用方式总结
2013/12/06 Javascript
js对象转json数组的简单实现案例
2014/02/28 Javascript
javascript和jquery实现设置和移除文本框默认值效果代码
2015/01/13 Javascript
使用jQueryMobile实现滑动翻页效果的方法
2015/02/04 Javascript
bootstrap table复杂操作代码
2016/11/01 Javascript
VUE实现日历组件功能
2017/03/13 Javascript
使用vue框架 Ajax获取数据列表并用BootStrap显示出来
2017/04/24 Javascript
详解npm 配置项registry修改为淘宝镜像
2018/09/07 Javascript
在vue里使用codemirror遇到的问题
2018/11/01 Javascript
记一次vue去除#问题处理经过小结
2019/01/24 Javascript
jQuery ajax仿Google自动提示SearchSuggess功能示例
2019/03/28 jQuery
js实现旋转木马轮播图效果
2020/01/10 Javascript
Python Deque 模块使用详解
2014/07/04 Python
Python定时器实例代码
2017/11/01 Python
python爬取cnvd漏洞库信息的实例
2019/02/14 Python
python GUI库图形界面开发之PyQt5单选按钮控件QRadioButton详细使用方法与实例
2020/02/28 Python
Python基于read(size)方法读取超大文件
2020/03/12 Python
介绍CSS3使用技巧5个
2009/04/02 HTML / CSS
护理学毕业生求职信
2013/11/14 职场文书
服装机修工岗位职责
2013/12/26 职场文书
消防器材管理制度
2014/01/28 职场文书
党的群众路线教育实践活动个人整改措施范文
2014/11/04 职场文书
致运动员加油稿
2015/07/21 职场文书
Python实现查询剪贴板自动匹配信息的思路详解
2021/07/09 Python
安装Windows Server 2012 R2企业版操作系统并设置好相关参数
2022/04/29 Servers