Python 3 – os.stat() 用法实例


os.stat() 方法用于在给定的路径上执行一个系统 stat 的调用。

os.stat(path)

path 文件路径
stat 结构:
st_mode: inode 保护模式
st_ino: inode 节点号。
st_dev: inode 驻留的设备。
st_nlink: inode 的链接数。
st_uid: 所有者的用户ID。
st_gid: 所有者的组ID。
st_size: 普通文件以字节为单位的大小;包含等待某些特殊文件的数据。
st_atime: 上次访问的时间。
st_mtime: 最后一次修改的时间。
st_ctime: 由操作系统报告的"ctime"。在某些系统上(如Unix)是最新的元数据更改的时间,在其它系统上(如Windows)是创建时间(详细信息参见平台的文档)。

Python
In [38]: import <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/os" title="View all posts in os" target="_blank">os</a></span> In [39]: <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/os" title="View all posts in os" target="_blank">os</a></span>.stat Out[39]: <function posix.stat> In [40]: os.stat('vector2d.py') Out[40]: os.stat_result(st_mode=33188, st_ino=8595328880, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=509, st_atime=1511089982, st_mtime=1509706352, st_ctime=1509706352) In [41]: os.stat('vector2d.py').st_size # 普通文件以字节为单位的大小;包含等待某些特殊文件的数据。 Out[41]: 509 In [42]: os.stat('vector2d.py').st_mode # inode 保护模式 Out[42]: 33188 In [43]: os.stat('vector2d.py').st_atime # 上次访问的时间。 Out[43]: 1511089982.6010573 In [46]: import time #将时间戳转化为localtime In [47]: x = time.localtime(os.stat('vector2d.py').st_atime) In [48]: time.strftime('%Y-%m-%d %H:%M:%S',x) Out[48]: '2017-11-19 19:13:02' """ x = time.localtime(os.stat('vector2d.py').st_atime) time.strftime('%Y-%m-%d %H:%M:%S',x) """ In [44]: os.stat('vector2d.py').st_mtime Out[44]: 1509706352.772378 In [52]: oct(os.stat('vector2d.py').st_mode) # oct 返回一个整数的八进制表示。 Out[52]: '0o100644'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
In [ 38 ] : import os
 
In [ 39 ] : os . stat
Out [ 39 ] : < function posix . stat >
 
In [ 40 ] : os . stat ( 'vector2d.py' )
Out [ 40 ] : os . stat_result ( st_mode = 33188 , st_ino = 8595328880 , st_dev = 16777220 , st_nlink = 1 , st_uid = 501 , st_gid = 20 , st_size = 509 , st_atime = 1511089982 , st_mtime = 1509706352 , st_ctime = 1509706352 )
 
In [ 41 ] : os . stat ( 'vector2d.py' ) . st_size # 普通文件以字节为单位的大小;包含等待某些特殊文件的数据。
Out [ 41 ] : 509
 
In [ 42 ] : os . stat ( 'vector2d.py' ) . st_mode # inode 保护模式
Out [ 42 ] : 33188
 
In [ 43 ] : os . stat ( 'vector2d.py' ) . st_atime # 上次访问的时间。
Out [ 43 ] : 1511089982.6010573
 
In [ 46 ] : import time
#将时间戳转化为localtime
In [ 47 ] : x = time . localtime ( os . stat ( 'vector2d.py' ) . st_atime )
 
In [ 48 ] : time . strftime ( '%Y-%m-%d %H:%M:%S' , x )
Out [ 48 ] : '2017-11-19 19:13:02'
 
"""
x = time.localtime(os.stat('vector2d.py').st_atime)
time.strftime('%Y-%m-%d %H:%M:%S',x)
"""
 
In [ 44 ] : os . stat ( 'vector2d.py' ) . st_mtime
Out [ 44 ] : 1509706352.772378
 
In [ 52 ] : oct ( os . stat ( 'vector2d.py' ) . st_mode ) # oct 返回一个整数的八进制表示。
Out [ 52 ] : '0o100644'



  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值