Python| shutil.disk_usage() 方法
Python中的Shutil 模块提供了许多对文件和文件集合进行高级操作的功能。它属于 Python 的标准实用程序模块。该模块有助于自动复制和删除文件和目录的过程。
Python中的shutil.disk_usage()
方法是获取给定路径的磁盘使用统计信息。此方法返回一个命名元组,其属性为total 、 used和free 。 total属性表示总空间量, used属性表示已用空间量, free属性表示可用空间量,以字节为单位。
注意:在 Windows 上,给定的路径必须代表一个目录,但在 Unix 系统上,它可以是一个文件或目录。
Syntax: shutil.disk_usage(path)
Parameter:
path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.
Return Type: This method returns a named tuple with attributed total, used and free.
代码: shutil.disk_usage() 方法的使用
# Python program to explain shutil.disk_usage() method
# importing shutil module
import shutil
# Path
path = "/home/User/Documents"
# Get the disk usage statistics
# about the given path
stat = shutil.disk_usage(path)
# Print disk usage statistics
print("Disk usage statistics:")
print(stat)
输出:
Disk usage statistics:
usage(total=244934381568, used=13350301696, free=219070689280)
参考: https://docs。 Python.org/3/library/shutil.html