Convert bytes to human-readable file sizes and back.
Project description
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
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.
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 |
is_larger_than(size, threshold) |
Compare size to human string |
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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_filesize-0.2.0.tar.gz.
File metadata
- Download URL: philiprehberger_filesize-0.2.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33d49ac27cb13732e8c810d4fe7ae3ac0ba690fa56df64199f76c3057acba094
|
|
| MD5 |
ac49536af802a62e0ee28bfd4c2a6837
|
|
| BLAKE2b-256 |
a4c7d6245ea12029377bba9644cda3cc009ade765ef35e5d060a257dd49f9099
|
File details
Details for the file philiprehberger_filesize-0.2.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_filesize-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3488bad070df90a311e07b7f8f76acc40d802c5c7c5a5299aea0ff5fd602ead
|
|
| MD5 |
c8842b55516a22dfb256f7b75fb03950
|
|
| BLAKE2b-256 |
2db0da93fde43b611d7758caa74db2d1b8ef1c4ed7622c095d3177acfc11fc75
|