humanfriendly

10.0 · active · verified Sat Mar 28

humanfriendly is a Python library that provides user-friendly output for text interfaces, including parsing and formatting numbers, file sizes, pathnames, and timespans. It also offers utilities for terminal interaction like text styling and prompting. The current version is 10.0, released in September 2021, with a release cadence that has included several minor and major updates in recent years and ongoing maintenance for Python compatibility on various platforms.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core functionality of parsing and formatting human-readable file sizes and timespans. It prompts the user for input and shows both decimal and binary size formatting. It also includes a comment on how to use the command-line demo feature.

import os
from humanfriendly import format_size, parse_size
from humanfriendly.prompts import prompt_for_input

# Example of parsing and formatting file sizes
user_input = prompt_for_input("Enter a human-readable file size (e.g., 16GB, 5MB): ")
num_bytes = parse_size(user_input)
print(f"Parsed bytes: {num_bytes}")
print(f"Formatted (decimal): {format_size(num_bytes)}")
print(f"Formatted (binary): {format_size(num_bytes, binary=True)}")

# Example of formatting a timespan
from humanfriendly import format_timespan
seconds = 3665 # 1 hour, 1 minute, 5 seconds
print(f"Formatted timespan: {format_timespan(seconds)}")

# To demonstrate CLI features like spinners (requires running in terminal):
# import subprocess
# subprocess.run(['humanfriendly', '--demo'])

view raw JSON →