aegis_sim.utilities.get_folder_size

 1import math
 2import os
 3
 4
 5def get_folder_size_with_du(folder_path):
 6    """Returns the folder size for Linux, macOS, and Windows."""
 7
 8    size = get_folder_size_python(folder_path)
 9    return size
10
11
12def convert_size(size_bytes):
13    """Converts bytes to a human-readable size format (e.g., KB, MB, GB)."""
14    if size_bytes == 0:
15        return "0B"
16    size_name = ("B", "KB", "MB", "GB", "TB")
17    i = int(math.floor(math.log(size_bytes, 1024)))
18    p = math.pow(1024, i)
19    s = round(size_bytes / p, 2)
20    return f"{s} {size_name[i]}"
21
22
23def get_folder_size_python(folder_path):
24    """Returns the total size of the folder in a human-readable format."""
25    total_size = 0
26
27    # Traverse the directory tree
28    for dirpath, dirnames, filenames in os.walk(folder_path):
29        for filename in filenames:
30            # Get the full file path
31            filepath = os.path.join(dirpath, filename)
32            # Skip if it's a symbolic link
33            if not os.path.islink(filepath):
34                # Accumulate file size
35                total_size += os.path.getsize(filepath)
36
37    return convert_size(total_size)
def get_folder_size_with_du(folder_path):
 6def get_folder_size_with_du(folder_path):
 7    """Returns the folder size for Linux, macOS, and Windows."""
 8
 9    size = get_folder_size_python(folder_path)
10    return size

Returns the folder size for Linux, macOS, and Windows.

def convert_size(size_bytes):
13def convert_size(size_bytes):
14    """Converts bytes to a human-readable size format (e.g., KB, MB, GB)."""
15    if size_bytes == 0:
16        return "0B"
17    size_name = ("B", "KB", "MB", "GB", "TB")
18    i = int(math.floor(math.log(size_bytes, 1024)))
19    p = math.pow(1024, i)
20    s = round(size_bytes / p, 2)
21    return f"{s} {size_name[i]}"

Converts bytes to a human-readable size format (e.g., KB, MB, GB).

def get_folder_size_python(folder_path):
24def get_folder_size_python(folder_path):
25    """Returns the total size of the folder in a human-readable format."""
26    total_size = 0
27
28    # Traverse the directory tree
29    for dirpath, dirnames, filenames in os.walk(folder_path):
30        for filename in filenames:
31            # Get the full file path
32            filepath = os.path.join(dirpath, filename)
33            # Skip if it's a symbolic link
34            if not os.path.islink(filepath):
35                # Accumulate file size
36                total_size += os.path.getsize(filepath)
37
38    return convert_size(total_size)

Returns the total size of the folder in a human-readable format.