philiprehberger-filesize
Convert bytes to human-readable file sizes and back.
Installation
pip install philiprehberger-filesize
Usage
from philiprehberger_filesize import humanize, parse, is_larger_than
humanize(1536) # "1.5 KB"
humanize(1073741824) # "1.0 GB"
humanize(1024, binary=True) # "1.0 KiB"
parse("1.5 GB") # 1500000000
parse("1 KiB") # 1024
is_larger_than(5000000, "1 MB") # True
Convert to a specific unit
to_unit(size, unit) returns a float in the requested unit. Useful for arithmetic when a formatted string would not do.
from philiprehberger_filesize import to_unit
to_unit(1500, "KB") # 1.5
to_unit(1024 ** 2, "MiB") # 1.0
to_unit(2_500_000, "MB") # 2.5
Convert from a unit to bytes
from_unit(value, unit) is the inverse of to_unit: it converts a numeric value in a named unit to bytes.
from philiprehberger_filesize import from_unit
from_unit(1.5, "MB") # 1_500_000
from_unit(2, "KiB") # 2048
from_unit(1, "GB") # 1_000_000_000
Size constants
Exported integer constants for use as multipliers in code:
from philiprehberger_filesize import KB, MB, GB, MIB, GIB, humanize
threshold = 5 * MB
humanize(threshold) # "5.0 MB"
humanize(2 * GIB, binary=True) # "2.0 GiB"
Available: BYTES, KB, MB, GB, TB, KIB, MIB, GIB, TIB.
Summing and comparing sizes
total(*sizes) accepts any mix of integer byte counts and human-readable strings, parses the strings via parse(), and returns the sum. compare(a, b) returns -1, 0, or 1 after parsing each operand — useful for sorting mixed int/str size values.
from philiprehberger_filesize import total, compare
total(1024, "1 KB", "2 MB") # 2_001_024
total() # 0
compare(100, 200) # -1
compare("1 KiB", 1024) # 0
compare("1 MB", "1 KB") # 1
sizes = ["10 KB", "1 MB", 500]
sorted(sizes, key=lambda s: compare(s, 0))
# [500, '10 KB', '1 MB']
API
| Function / Class | Description |
|---|---|
humanize(size, binary=False, precision=1) |
Bytes to human string |
format_bytes(size, binary=False, precision=2) |
Alias with precision=2 |
parse(text) |
Human string to bytes |
to_unit(size, unit) |
Convert bytes to a specific unit, returning a float |
from_unit(value, unit) |
Convert a value in a named unit to bytes (inverse of to_unit) |
is_larger_than(size, threshold) |
Compare size to human string |
total(*sizes) |
Sum mixed int/string sizes into bytes |
compare(a, b) |
Return -1/0/1 after parsing both operands |
BYTES, KB, MB, GB, TB |
SI multiplier constants (1000-based) |
KIB, MIB, GIB, TIB |
Binary multiplier constants (1024-based) |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
License
Metadata
Release files for philiprehberger-filesize 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| philiprehberger_filesize-0.4.0.tar.gz | 178.1 kB | Without provenance | Details for philiprehberger_filesize-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| philiprehberger_filesize-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for philiprehberger_filesize-0.4.0-py3-none-any.whl |
Total release size: 184.1 kB
Release files / philiprehberger_filesize-0.4.0.tar.gz
| Download URL | philiprehberger_filesize-0.4.0.tar.gz |
|---|---|
| Size | 178.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
682d1862548d63aebe0fd3aaa3e93fdbf47007743b3844ea81bc06a14f8120b0
|
|
BLAKE2b-256 checksum How to use checksums |
5deb2e320b60c55daea780e2d0a334d2eeac8c1694c4307b6ddb42fc7043699f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Release files / philiprehberger_filesize-0.4.0-py3-none-any.whl
| Download URL | philiprehberger_filesize-0.4.0-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d53831e8ab6002657333cca651eeed702b5f7f79d74051357b8f33447bdb06f8
|
|
BLAKE2b-256 checksum How to use checksums |
81302c388f0618abe87e8762441311bb376882b88fdfd33baeb6e64175d562d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Security for philiprehberger-filesize 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.