Utility functions
- diskinfo._read_file(path, encoding='utf-8')[source]
Reads the text content of the specified file. The function will hide
IOError
andFileNotFound
exceptions during the file operations. The result bytes will be read with the specified encoding and stripped.- Parameters:
- Returns:
file content text
- Return type:
Example
An example aboout the use of the function:
>>> from diskinfo import * >>> _read_file("/sys/block/sda/dev") '8:0'
- diskinfo._read_udev_property(path, udev_property, encoding='utf-8')[source]
Reads a property from an udev data file. The function will hide
IOError
and py:obj:FileNotFound exceptions during the file operations. The result string will be decoded and stripped.- Parameters:
- Returns:
udev property value
- Return type:
- Raises:
ValueError – in case of empty input parameters
Example
An example about the use of the function:
>>> from diskinfo import * >>> _read_udev_property("/run/udev/data/b259:0", "ID_MODEL=") 'WDS100T1X0E-00AFY0'
- diskinfo._read_udev_path(path, path_type, encoding='utf-8')[source]
Reads one or more path elements from an udev data file. It will hide
IOError
andFileNotFound
exceptions during the file operations. The result path elements will be decoded and stripped.- Parameters:
- Returns:
path elements
- Return type:
List[str]
- Raises:
ValueError – in case of empty or invalid input parameters
Example
An example about the use of the function:
>>> from diskinfo import * >>> _read_udev_path("/run/udev/data/b259:0", 1) ['/dev/disk/by-path/pci-0000:02:00.0-nvme-1']
- diskinfo.size_in_hrf(size_value, units=0)[source]
Returns the size in a human-readable form.
- Parameters:
size_value (int) – number of bytes
units (int) –
unit system will be used for the calculation and in the result:
0 metric units (default)
1 IEC units
2 legacy units
Read more about units here.
- Returns:
size in human-readable form, proper unit
- Return type:
- Raises:
ValueError – in case of invalid input parameters (negative size, invalid units)
Example
An example about the use of the function:
>>> from diskinfo import * >>> size = 12839709879873 >>> s, u = size_in_hrf() >>> print(f"{s:.1f} {u}") 12.8 TB >>> s, u = size_in_hrf(size, units=1) >>> print(f"{s:.1f} {u}") 11.7 TiB
- diskinfo.time_in_hrf(time, unit=0, short_format=False)[source]
Returns the amount of time in a human-readable form.
- Parameters:
- Returns:
time in human-readable form, proper unit
- Return type:
- Raises:
ValueError – in case of invalid input parameters (negative time, invalid unit)
Example
An example about the use of the function:
>>> from diskinfo import * >>> hours = 6517 >>> t, u = time_in_hrf(hours, unit=2) >>> print(f"{t:.1f} {u}") 271.5 day >>> days = 2401 >>> t, u = time_in_hrf(hours, unit=3, short_format=True) >>> print(f"{t:.1f} {u}") 6.6 yr