Skip to content

Instantly share code, notes, and snippets.

@Pymmdrza
Created August 24, 2024 17:18
Show Gist options
  • Select an option

  • Save Pymmdrza/c8918a425bd6cccf451e755134e4ebbe to your computer and use it in GitHub Desktop.

Select an option

Save Pymmdrza/c8918a425bd6cccf451e755134e4ebbe to your computer and use it in GitHub Desktop.
Natural File Size Convertor on Python
def naturalsize(num_bytes: int) -> str:
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
if num_bytes < 1024.0:
return f"{num_bytes:.2f} {unit}"
num_bytes /= 1024.0
return f"{num_bytes:.2f} PB"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment