What is the psutil.disk_usage() method in Python?
The psutil module
psutil (Python system and process utilities) is a Python package that retrieves information on ongoing processes and system usage (CPU, memory, storage, network, and sensors).
It is mostly used for system monitoring, profiling, restricting process resources, and process management.
The module can be installed via pip as follows:
pip install psutil
disk_usage method
The sensors_fans method returns the disk usage statistics about the partition of the given path as a named tuple. The named tuple consists of the following attributes:
total: This represents the total amount of memory.used: This represents used memory.free: This represents free memory.percent: This represents the percentage usage of the memory.
All the above attributes are reported in bytes.
Syntax
psutil.disk_usage(path)
path: This is the file path.
Parameter
The method has no parameters.
Code
import psutil, osprint("The usage statistics of " + os.getcwd() + " is:")print(psutil.disk_usage(os.getcwd()))
Explanation
- Line 1: We import the
psutilandosmodules. - Line 4: We use the
psutil.disk_usage()method to retrieve the usage statistics of the current working directory, and pass the current working directory as the file path. We use theos.getcwd()method to obtain the current working directory.