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

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)
3876301
3.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)

附:月份缩写 -_-||
在这里插入图片描述

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 当前时区的名称
  • %% %号本身
  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 非常感谢你的提问。关于你的问题,Python 如何获取生成文件的大小,可以使用 os 模块的 stat 方法,其中 st_size 属性可以获取文件的大小,以字节为单位。以下是一个示例代码: ``` import os filename = 'example.txt' # 获取文件大小(字节数) filesize = os.stat(filename).st_size print(f'文件 {filename} 的大小为 {filesize} 字节') ``` 当然,如果你有其他问题,也欢迎继续提问。 ### 回答2: 在Python中可以通过使用os模块来获取生成文件的大小。 首先,需要引入os模块:import os 然后,可以使用os.path.getsize()函数来获取文件的大小,该函数需要传入文件的路径作为参数,返回文件的大小(以字节为单位)。 例如,假设需要获取名为myfile.txt的文件的大小,可以使用以下代码: file_size = os.path.getsize('myfile.txt') 最后,可以将文件大小转换为其他单位,如KB、MB等,以便更好地理解文件大小。可以通过除以1024来将文件大小从字节转换为KB,再次除以1024将文件大小从KB转换为MB。 例如,以下代码将文件大小从字节转换为KB和MB,并显示在控制台上: file_size_kb = file_size / 1024 file_size_mb = file_size_kb / 1024 print("文件大小(KB):", file_size_kb) print("文件大小(MB):", file_size_mb) 这样就可以通过使用os模块中的函数来获取生成文件的大小,并且可以转换为其他单位进行显示。 ### 回答3: 在Python中,可以使用`os`模块来获取生成文件的大小。下面是一种常见的方法: ```python import os # 获取文件路径 file_path = "文件路径" # 获取文件大小(字节) file_size = os.path.getsize(file_path) print("文件大小:", file_size, "字节") ``` 以上代码中,首先通过`os.path.getsize()`函数获取文件的大小,该函数会返回文件的大小(以字节为单位)。然后,打印出文件的大小。 需要注意的是,`file_path`应该是一个有效的文件路径,指向所需获取大小的文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值