Utility functions

diskinfo._read_file(path, encoding='utf-8')[source]

Reads the text content of the specified file. The function will hide IOError and FileNotFound exceptions during the file operations. The result bytes will be read with the specified encoding and stripped.

Parameters:
  • path (str) – file path

  • encoding (str) – encoding (default is utf-8)

Returns:

file content text

Return type:

str

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:
  • path (str) – path of the udev data file (e.g. /run/udev/data/b8:0)

  • udev_property (str) – udev property string

  • encoding (str) – encoding (default is utf-8)

Returns:

udev property value

Return type:

str

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 and FileNotFound exceptions during the file operations. The result path elements will be decoded and stripped.

Parameters:
  • path (str) – path of the udev data file (e.g. /run/udev/data/b8:0)

  • path_type (int) –

    type of the path to find/load from udev data file. Valid values are:

    • 0 by-id path

    • 1 by-path path

    • 2 by-partuuid path

    • 3 by-partlabel path

    • 4 by-label path

    • 5 by-uuid path

  • encoding (str) – encoding (default is utf-8)

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:

Tuple[float, str]

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:
  • time (int) – time value

  • unit (int) –

    unit of the input time value

    • 0 seconds

    • 1 minutes

    • 2 hours

    • 3 days

    • 4 years

  • short_format (bool) – result unit in short format (e.g. min instead of minute)

Returns:

time in human-readable form, proper unit

Return type:

Tuple[float, str]

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