bitmath

1.3.3.1 · maintenance · verified Thu Apr 16

bitmath is a Python library (version 1.3.3.1) for representing, converting, and manipulating file sizes with different prefix notations (SI and NIST). It provides functionality for unit conversion (e.g., kB to GiB), arithmetic operations, rich comparisons, and human-readable formatting. The library's core functionality is stable and widely used for precise file size handling, though its release cadence has stalled.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to instantiate bitmath objects, convert between units, perform basic arithmetic, and parse string representations of file sizes.

import bitmath

# Instantiate some bitmath objects
file_size_mib = bitmath.MiB(100) # 100 Mebibytes
network_speed_mb = bitmath.MB(500) # 500 Megabytes

# Convert units
file_size_kib = file_size_mib.to_KiB() # Convert to Kibibytes
print(f"100 MiB is {file_size_kib}")

# Perform arithmetic
total_data = file_size_mib + bitmath.GiB(2)
print(f"100 MiB + 2 GiB = {total_data.best_prefix()}")

# Parse a string
parsed_size = bitmath.parse_string("1.5 TiB")
print(f"Parsed size: {parsed_size}")

view raw JSON →