Filesize
Bits and bytes related humanization.
naturalsize(value, binary=False, gnu=False, format='%.1f')
Format a number of bytes like a human-readable filesize (e.g. 10 kB).
By default, decimal suffixes (kB, MB) are used.
Non-GNU modes are compatible with jinja2's filesizeformat
filter.
Examples:
>>> naturalsize(3000000)
'3.0 MB'
>>> naturalsize(300, False, True)
'300B'
>>> naturalsize(3000, False, True)
'2.9K'
>>> naturalsize(3000, False, True, "%.3f")
'2.930K'
>>> naturalsize(3000, True)
'2.9 KiB'
>>> naturalsize(10**28)
'10.0 RB'
>>> naturalsize(10**34 * 3)
'30000.0 QB'
>>> naturalsize(-4096, True)
'-4.0 KiB'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
(int, float, str)
|
Integer to convert. |
required |
binary |
bool
|
If |
False
|
gnu |
bool
|
If |
False
|
format |
str
|
Custom formatter. |
'%.1f'
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Human readable representation of a filesize. |
Source code in .tox/docs/lib/python3.12/site-packages/humanize/filesize.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|