Python| os.fstat() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.fstat()
方法用于获取文件描述符的状态。
文件描述符是与当前进程已打开的文件相对应的小整数值。
文件描述符指示资源并充当句柄以执行各种较低级别的 I/O 操作,如读取、写入、发送等。
例如:标准输入通常是值为 0 的文件描述符,标准输出通常是值为 1 的文件描述符,标准错误通常是值为 2 的文件描述符。
当前进程打开的其他文件将获得值 3、4、5 等等。
os.fstat()
方法等价于os.stat(fd)
方法。
Syntax: os.fstat(fd)
Parameter:
fd: A file descriptor.
Return Type: This method returns a stat_result object of class os which represents the status of the given file descriptor.
The returned ‘stat_result’ object is a tuple which has following named attributes:
- st_mode: It represents file type and file mode bits (permissions).
- st_ino: It represents the inode number on Unix and the file index on Windows platform.
- st_dev: It represents the identifier of the device on which this file resides.
- st_nlink: It represents the number of hard links.
- st_uid: It represents the user identifier of the file owner.
- st_gid: It represents the group identifier of the file owner.
- st_size: It represents the size of the file in bytes.
- st_atime: It represents the time of most recent access. It is expressed in seconds.
- st_mtime: It represents the time of most recent content modification. It is expressed in seconds.
- st_ctime: It represents the time of most recent metadata change on Unix and creation time on Windows. It is expressed in seconds.
- st_atime_ns: It is same as st_atime but the time is expressed in nanoseconds as an integer.
- st_mtime_ns: It is same as st_mtime but the time is expressed in nanoseconds as an integer.
- st_ctime_ns: It is same as st_ctime but the time is expressed in nanoseconds as an integer.
- st_blocks: It represents the number of 512-byte blocks allocated for file.
- st_rdev: It represents the type of device, if an inode device.
- st_flags: It represents the user defined flags for file.
Note: Some attributes are platform dependent and are subject to availability.
代码:使用 os.fstat() 方法获取文件描述符的状态
# Python program to explain os.fstat() method
# importing os module
import os
# Path
path = "/home / ihritik / Desktop / file1.txt"
# open the file represented by
# the above given path and get
# the file descriptor associated
# with it using os.open() method
fd = os.open(path, os.O_RDONLY)
# Get the status of the
# file descriptor using
# os.fstat() method
status = os.fstat(fd)
# Print the status of
# the file descriptor
print(status)
# close the file descriptor
os.close(fd)
os.stat_result(st_mode=33188, st_ino=801111, st_dev=2056, st_nlink=1, st_uid=1000,
st_gid=1000, st_size=6550, st_atime=1560377051, st_mtime=1560377051, st_ctime=1560377051)