DiskInfo class

class diskinfo.DiskInfo[source]

This class implements disk exploration functionality. At class initialization time all existing disks will be explored automatically (empty loop devices will be skipped). In a next step, get_disk_number() method can return the number of identified disk and get_disk_list() method can return the list of the identified disks. In both cases disk type filters can be applied to get only a subset of the discovered disks. The filters are set of DiskType values.

Operator in is also implemented for this class. Caller can check if a Disk class instance can be found on the list of the identified disks.

Example

A code example about the basic use of the class and the use of the in operator.

>>> from diskinfo import Disk, DiskType, DiskInfo
>>> di = DiskInfo()
>>> n = di.get_disk_number(included={DiskType.SSD}, excluded={DiskType.HDD})
>>> print(f"Number of SSDs: {n}")
Number of SSDs: 3
>>> d = Disk("sda")
>>> print(d in di)
True
get_disk_list(included=None, excluded=None, sorting=False, rev_order=False)[source]

Returns the list of identified disks. The caller can specify inclusive and exclusive filters for disk types. If no filters are specified the default behavior is to include all disk types and to exclude nothing. The list can be sorted based on the disk name in alphabetical order. Caller can also request sorting in reverse order.

Parameters:
  • included (set) – filter set for included disk types

  • excluded (set) – filter set for excluded disk types

  • sorting (bool) – disk list will be sorted based on name string

  • rev_order (bool) – sorting in reverse order

Returns:

list of the (filtered) disks

Return type:

List[Disk]

Raises:

ValueError – if the same disk type is on both included and excluded filter sets

Example

A code example about using filters and sorting: it will list the device path of the sorted list of the HDDs:

>>> from diskinfo import DiskType, DiskInfo
>>> di = DiskInfo()
>>> disks = di.get_disk_list(included={DiskType.HDD}, sorting=True)
>>> for d in disks:
...     print(d.get_path())
...
/dev/sda
/dev/sdb
/dev/sdc
get_disk_number(included=None, excluded=None)[source]

Returns the number of the disks. The caller can specify inclusive and exclusive filters for disk types. If no filters are specified then the default behavior is to include all disk types and to exclude nothing.

Parameters:
  • included (set) – filter set for included disk types

  • excluded (set) – filter set for excluded disk types

Returns:

number of the (filtered) disks

Return type:

int

Raises:

ValueError – if the same disk type is on both included and excluded filter sets

Example

A code example about using filters: it counts the number of SSDs excluding NVME disks.

>>> from diskinfo import DiskType, DiskInfo
>>> di = DiskInfo()
>>> n = di.get_disk_number(included={DiskType.SSD}, excluded={DiskType.HDD})
>>> print(f"Number of SSDs: {n}")
Number of SSDs: 3