function format_file_size
Converts a file size in bytes to a human-readable string format with appropriate units (B, KB, MB, or GB).
/tf/active/vicechatdev/e-ink-llm/cloudtest/analyze_replica.py
70 - 79
simple
Purpose
This function takes a file size expressed in bytes and formats it into a more readable representation by automatically selecting the most appropriate unit (bytes, kilobytes, megabytes, or gigabytes). It uses binary units (1024-based) and formats decimal values to one decimal place for units larger than bytes. This is commonly used in file managers, download progress indicators, storage displays, and any application that needs to present file sizes to users in an intuitive format.
Source Code
def format_file_size(size_bytes: int) -> str:
"""Format file size in human readable format"""
if size_bytes < 1024:
return f"{size_bytes} B"
elif size_bytes < 1024 * 1024:
return f"{size_bytes / 1024:.1f} KB"
elif size_bytes < 1024 * 1024 * 1024:
return f"{size_bytes / (1024 * 1024):.1f} MB"
else:
return f"{size_bytes / (1024 * 1024 * 1024):.1f} GB"
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
size_bytes |
int | - | positional_or_keyword |
Parameter Details
size_bytes: An integer representing the file size in bytes. Expected to be a non-negative integer value. The function will convert this value to the most appropriate unit (B, KB, MB, or GB) based on its magnitude. Values less than 1024 are displayed in bytes, 1024-1048575 in KB, 1048576-1073741823 in MB, and 1073741824 or greater in GB.
Return Value
Type: str
Returns a string containing the formatted file size with one decimal place precision (for KB, MB, GB) and the appropriate unit suffix. For sizes less than 1024 bytes, returns an integer value with 'B' suffix. Examples: '512 B', '1.5 KB', '2.3 MB', '1.2 GB'. The format uses a space between the numeric value and the unit.
Usage Example
# Basic usage examples
size1 = format_file_size(500)
print(size1) # Output: '500 B'
size2 = format_file_size(2048)
print(size2) # Output: '2.0 KB'
size3 = format_file_size(5242880)
print(size3) # Output: '5.0 MB'
size4 = format_file_size(1073741824)
print(size4) # Output: '1.0 GB'
# Practical example with file operations
import os
file_path = 'example.txt'
if os.path.exists(file_path):
file_size = os.path.getsize(file_path)
readable_size = format_file_size(file_size)
print(f'File size: {readable_size}')
Best Practices
- This function uses binary units (1024-based) rather than decimal units (1000-based). Be aware of the difference between KB (kibibyte, 1024 bytes) and kB (kilobyte, 1000 bytes) when displaying to users.
- The function assumes non-negative input values. Consider adding input validation if negative values might be passed.
- For very large files (terabytes or larger), the function will display them in GB, which may result in large numbers. Consider extending the function to support TB and PB units if needed.
- The function returns one decimal place for all units except bytes. This provides a good balance between precision and readability for most use cases.
- Consider caching the result if the same file size needs to be formatted multiple times to avoid redundant calculations.
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function format_file_size_v1 95.0% similar
-
function format_file_size_v1 93.1% similar
-
function human_readable_size 79.5% similar
-
function calculate_file_hash_v1 42.7% similar
-
function _int_to_bytes 42.6% similar