python中entry的用法_Python os.DirEntry.stat()用法及代码示例

Python中的OS模块提供了与操作系统进行交互的功能。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的功能的便携式方法。

os.scandir()os模块产量的方法os.DirEntry与指定路径给定目录中的条目相对应的对象。os.DirEntry对象具有各种属性和方法,用于公开目录条目的文件路径和其他文件属性。

stat()方法开启os.DirEntryobject用于获取条目的os.stat_result对象。

注意: os.DirEntry打算在迭代后使用和丢弃对象,因为对象的属性和方法将其值缓存起来,而不再重新获取这些值。自调用os.scandir()方法以来,文件的元数据是否已更改,或者是否经过了很长时间。我们将不会获得up-to-date信息。

用法: os.DirEntry.stat(*, follow_symlinks = True)

参数:

follow_symlinks:此参数需要一个布尔值。如果条目是符号链接,并且follow_symlinks为True,则该方法将在符号链接指向的路径上操作。如果条目是符号链接,并且follow_symlinks为False,则该方法将在符号链接本身上运行。如果该条目不是符号链接,则将忽略follow_symlinks参数。此参数的默认值为True。

返回值:此方法返回条目的os.stat_result对象。以下是os.stat_result对象的属性:

st_mode:表示文件类型和文件模式位(权限)。

st_ino:它表示Unix上的inode编号和Windows平台上的文件索引。

st_dev:它代表此文件所在设备的标识符。

st_nlink:它表示硬链接的数量。

st_uid:它代表文件所有者的用户标识符。

st_gid:它代表文件所有者的组标识符。

st_size:它表示文件的大小(以字节为单位)。

st_atime:它表示最近访问的时间。以秒为单位。

st_mtime:它表示最近一次内容修改的时间。以秒为单位。

st_ctime:它表示Unix上最近的元数据更改时间以及Windows上的创建时间。以秒为单位。

st_atime_ns:与st_atime相同,但是时间以纳秒为单位表示为整数。

st_mtime_ns:与st_mtime相同,但时间以纳秒为单位表示为整数。

st_ctime_ns:与st_ctime相同,但时间以纳秒为单位表示为整数。

st_blocks:代表分配给文件的512字节块的数量。

st_rdev:表示设备类型,如果是inode设备。

st_flags:它代表用户定义的文件标志。

码:用于os.DirEntry.stat()方法

# Python program to explain os.DirEntry.stat() method

# importing os module

import os

# Directory to be scanned

# Path

path = "/home / ihritik"

# Print status of all

# files in the above

# specified path

# Using os.scandir() method

# scan the specified directory

# and yield os.DirEntry object

# for each file and sub-directory

print("Status of all files in path '% s':" % path)

with os.scandir(path) as itr:

for entry in itr:

# Check if the entry

# is a file

if entry.is_file():

# Print file status

print("Status of % s:" % entry.name)

print(entry.stat(), "\n")

输出:

Status of all files in path '/home/ihritik':

Status of file.txt:

os.stat_result(st_mode=33248, st_ino=801366, st_dev=2056, st_nlink=2, st_uid=1000,

st_gid=1000, st_size=409, st_atime=1566360293, st_mtime=1566287810,

st_ctime=1566291428)

Status of tree.cpp:

os.stat_result(st_mode=33188, st_ino=801364, st_dev=2056, st_nlink=1, st_uid=1000,

st_gid=1000, st_size=820, st_atime=1565604415, st_mtime=1565604415,

st_ctime=1565604415)

Status of graph.cpp:

os.stat_result(st_mode=33188, st_ino=801237, st_dev=2056, st_nlink=1, st_uid=1000,

st_gid=1000, st_size=1729, st_atime=1561515200, st_mtime=1561515069,

st_ctime=1561515069)

Status of abc.txt

os.stat_result(st_mode=33434, st_ino=801196, st_dev=2056, st_nlink=1, st_uid=1000,

st_gid=1000, st_size=0, st_atime=1560204341, st_mtime=1560204341,

st_ctime=1560204349)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值