What is the psutil.disk_partitions method in Python?

What is the psutil module?

In Python, psutil (Python system and process utilities) is a 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

The disk_partitions method

The disk_partitions method retrieves information on all the mounted disk partitions. This method returns a named tuple with the following attributes:

  • device: This represents the device path.
  • mountpoint: This represents the mount point path.
  • fstype: This represents the partition filesystem, for example, “ext3” on UNIX or “NTFS” on Windows.
  • opts: This represents a comma-separated string indicating different mount options for the drive/partition. This attribute is platform-dependent.
  • maxfile: This represents the maximum length a file name can have.
  • maxpath: This represents the maximum length a path name (including the directory name and the base filename) can have.

Syntax

psutil.disk_partitions(all=False)
  • all: If this is False, the method tries to differentiate and return only physical devices (such as hard discs, CD-ROM drives, and USB keys). It ignores everything else (e.g., pseudo, memory, duplicate, inaccessible filesystems).

Code

import psutil
print("All disk partitions: ")
print(psutil.disk_partitions())

Explanation

  • Line 1: We import the psutil module.
  • Line 5: We retrieve the information about the mounted disk partitions using the psutil.disk_partitions() method.

Free Resources