The File Size Converter module provides functions for converting file sizes between different units in both IEC (binary) and SI (decimal) formats. It includes functionality for getting the full name of a unit from its abbreviation and converting sizes between units.
pip install wolfsoftware.convert-sizeimport wolfsoftware.convert_size as fscTo convert a size between different units, use the convert_size function. You can specify whether to use SI units by setting the si_units parameter.
# IEC conversion example
size_in_gib = fsc.convert_size(1024, 'MiB', 'GiB')
print(size_in_gib) # Output: 1.0
# SI conversion example
size_in_gb = fsc.convert_size(1000, 'MB', 'GB', si_units=True)
print(size_in_gb) # Output: 1.0To get the full name of a unit from its abbreviation, use the get_name_from_code function.
# IEC unit name example
unit_name = fsc.get_name_from_code('KiB')
print(unit_name) # Output: Kibibyte
# SI unit name example
unit_name = fsc.get_name_from_code('KB', si_units=True)
print(unit_name) # Output: Kilobyteconvert_size(size: float, start_unit: str, end_unit: str, si_units: bool = False) -> floatConvert a size between units, either IEC or SI.
size (float): The original size.start_unit (str): The starting unit abbreviation.end_unit (str): The ending unit abbreviation.si_units (bool): If True, use SI units; otherwise, use IEC units.ValueError: If the unit abbreviations are not valid.get_name_from_code(unit: str, si_units: bool = False) -> strGet the full name of a unit from its abbreviation.
unit (str): The unit abbreviation.si_units (bool): If True, use SI units; otherwise, use IEC units.ValueError: If the unit abbreviation is not valid.get_name_from_code_iec(unit: str) -> strGet the full name of an IEC unit from its abbreviation.
unit (str): The IEC unit abbreviation.ValueError: If the unit abbreviation is not valid.get_name_from_code_si(unit: str) -> strGet the full name of an SI unit from its abbreviation.
unit (str): The SI unit abbreviation.ValueError: If the unit abbreviation is not valid.The module raises ValueError exceptions for invalid unit abbreviations. Ensure that you handle these exceptions in your code:
try:
size_in_gib = fsc.convert_size(1024, 'MiB', 'GiB')
except ValueError as e:
print(e)