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 disks andget_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 ofDiskTypevalues.Operator
inis also implemented for this class. Caller can check if aDiskclass instance can be found on the list of the identified disks.Example
An example for the basic use of the class and with
inoperator.>>> 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:
- 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
An example for 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:
- Returns:
number of the (filtered) disks
- Return type:
- Raises:
ValueError – if the same disk type is on both included and excluded filter sets
Example
An example for using filters: it counts the number of SSDs excluding HDDs.
>>> 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