Last modified: Jun 16, 2023 By Alexander Williams

Working With hurry Filesize in Python

"Hurry.filesize" is a Python library that provides functionality to convert file sizes into human-readable formats. 

In this guide, you’ll see how this library can convert file sizes into easily understandable formats.

Installation

Before getting started, you'll need to install Hurry Filesize using the following command;

 pip install hurry.filesize

How to use Hurry Filesize

To see how Hurry Filesize works, let's see a basic example.

from hurry.filesize import size

file_size = 1024  # Size in bytes

# Convert file size to a human-readable format
formatted_size = size(file_size)

print(formatted_size)

Output:

1K

Well, the code converted a file size (1024 bytes) to a human-readable format, "1K" (kilobyte).

Let's see another example:

from hurry.filesize import size, alternative

file_size = 5368709120  # Size in bytes (5 GB)

# Convert file size to a human-readable format with default units
formatted_size = size(file_size)

print(formatted_size)

Output:

5G

In this example, we've converted a file size of 5 GB (5368709120 bytes).