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 about the use of the function:

>>> from diskinfo import *
>>> _read_file("/sys/block/sda/dev")
"8:0"
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(size)
>>> 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