Python| os.statvfs() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.statvfs()
方法用于获取包含给定路径的已挂载文件系统的信息。为了获取文件系统信息,该方法在给定路径上执行statvfs()系统调用。
注意: os.statvfs()
方法仅在 Unix 平台上可用。
Syntax: os.statvfs(path)
Parameter:
path: A path-like object for which file system information is required.
Return Type: This method returns an object of class ‘os.statvfs_result’ whose attributes represents the information about the file system containing the given path.
The returned os.statvfs_result object has following attributes:
- f_bsize: It represents the file system block size
- f_frsize: It represents the fragment size
- f_blocks It represents the size of fs in f_frsize units
- f_bfree: It represents the number of free blocks
- f_bavail: It represents the number of free blocks for unprivileged users
- f_files: It represents the number of inodes
- f_ffree: It represents the number of free inodes
- f_favail: It represents the number of free inodes for unprivileged users
- f_fsid: It represents the file system ID
- f_flag: It represents the mount flags
- f_namemax: It represents the maximum filename length
代码:使用 os.statvfs() 方法获取有关包含给定路径的文件系统的信息。
# Python program to explain os.statvfs() method
# importing os module
import os
# File path
path = "/home / ihritik / Desktop / file.txt"
# Get the information about the
# filesystem containing the
# given path using os.statvfs() method
info = os.statvfs(path)
# Print the information
# about the file system
print(info)
# Print the file system block size
print("File system block size:", info.f_bsize)
# Print the the number of free blocks
# in the file system
print("Number of free blocks:", info.f_bfree)
输出:
os.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=59798433, f_bfree=56521834,
f_bavail=53466807, f_files=15261696, f_ffree=14933520, f_favail=14933520, f_flag=4096,
f_namemax=255)
File system block size: 4096
Number of free blocks: 56517297