Python | os.statvfs 函数

最近更新时间 2020-12-09 12:09:43

os.statvfs 函数在所给的路径上执行 statvfs() 系统调用。返回值是一个对象,其属性描述了所给路径上的文件系统,并且与 statvfs 结构体的成员相对应,即:f_bsize, f_frsize, f_blocks, f_bfree, f_bavail, f_files, f_ffree, f_favail, f_flag, f_namemax, f_fsid。

为 f_flag 属性位定义了两个模块级常量:如果存在 ST_RDONLY 位,则文件系统以只读挂载;如果存在 ST_NOSUID 位,则文件系统禁用或不支持 setuid/setgid 位。

函数定义

os.statvfs(path)
# 函数定义

if sys.platform != 'win32':
    def statvfs(path: _FdOrPathType) -> statvfs_result: ...  # Unix only

# 返回值 os.statvfs_result 定义
if sys.platform != 'win32':
    class statvfs_result:  # Unix only
        f_bsize: int  # It represents the file system block size
        f_frsize: int # It represents the fragment size
        f_blocks: int # It represents the size of fs in f_frsize units
        f_bfree: int # It represents the number of free blocks 
        f_bavail: int # It represents the number of free blocks for unprivileged users
        f_files: int # It represents the number of inodes
        f_ffree: int # t represents the number of free inodes
        f_favail: int # It represents the number of free inodes for unprivileged users
        f_fsid: int # It represents the file system ID
        f_flag: int # It represents the mount flags
        f_namemax: int # It represents the maximum filename length

兼容性:Unix 系统。

参数

  • checkpath - 目录或文件路径。

返回值

  • checkstatvfs_result - 返回值参见函数定义。

示例1: - 使用 os.statvfs() 函数返回 statvfs() 系统调用对象,比如文件名最大值。

# coding=utf-8

# Python3 代码
# 讲解怎样使用 os.statvfs() 函数执行 statvfs() 系统调用

# 引入 os 库
import os

# 文件路径
path = "foo.txt"

# 执行 statvfs() 系统调用
statvfs = os.statvfs(path)

# 打印文件状态图
print(statvfs)

# 打印最大文件名
print("Name Max::", statvfs.f_namemax)
os.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=10255672, f_bfree=1571516, f_bavail=1043132, f_files=2621440, f_ffree=1660839, f_favail=1660839, f_flag=4096, f_namemax=255)
Name Max:: 255
为基于 GNU/glibc 的系统还定义了额外的模块级常量。它们是 ST_NODEV (禁止访问设备专用文件),ST_NOEXEC (禁止执行程序),ST_SYNCHRONOUS (写入后立即同步),ST_MANDLOCK (允许文件系统上的强制锁定),ST_WRITE (写入文件/目录/符号链接),ST_APPEND (仅追加文件),ST_IMMUTABLE (不可变文件),ST_NOATIME (不更新访问时间),ST_NODIRATIME (不更新目录访问时间),ST_RELATIME (相对于 mtime/ctime 更新访问时间)。
rss_feed