Last modified: Jun 02, 2023 By Alexander Williams

Python: Retrieving File Size in KB, MB, and GB

When working with files in Python, obtaining the file size and presenting it in a more easily understandable format is often essential.

In this tutorial, we will learn how to retrieve file sizes in kilobytes (KB), megabytes (MB), or gigabytes (GB) using Python's os module.

Using the os Module To get file size

The os module in Python offers a range of functions for file and directory operations, process management, and accessing environment variables. In our particular case, we can utilize the os.path.getsize() function to Retrieve the file size.

The os.path.getsize() function is a methodĀ used to retrieve the size of a file in bytes. Let's see how to use it an example:

import os

# Define the file path
file_path = "path/to/file.txt"

# Retrieve the size of the file
file_size = os.path.getsize(file_path)

# Print the file size
print(f"The size of the file is {file_size} bytes.")

As mentioned, the provided code prints the size of the given file in bytes.

To get the file size in kilobytes (KB), megabytes (MB), and gigabytes (GB), we can convert the result obtained from the os.path.getsize() function by dividing the file size by the appropriate conversion factors.

Here's an example function that converts the file size from bytes to kilobytes (KB), megabytes (MB), and gigabytes (GB):

import os

def get_file_size(file_path):
    # Retrieve the size of the file in bytes
    file_size = os.path.getsize(file_path)

    # Convert file size to kilobytes
    kb = file_size / 1024

    # Convert kilobytes to megabytes
    mb = kb / 1024

    # Convert megabytes to gigabytes
    gb = mb / 1024

    # Create a dictionary to store the file size in different units
    result = {
        "file_size": file_size,
        "kb": kb,
        "mb": mb,
        "gb": gb
    }

    # Return the dictionary with file size information
    return result

Now, let's proceed to test this function using this specific file.

file.zip

Here is the code:

print(get_file_size('file.zip'))

Output:

{'file_size': 1512980, 'kb': 1477.51953125, 'mb': 1.4428901672363281, 'gb': 0.0014090724289417267}

You can access the corresponding value using its key to get a specific size from the dictionary. Here's an example:

size_dict = get_file_size('file.zip')

# Access specific sizes from the dictionary
file_size = size_dict["file_size"]
kb_size = size_dict["kb"]
mb_size = size_dict["mb"]
gb_size = size_dict["gb"]

# Print the specific sizes
print(f"File size: {file_size} bytes")
print(f"Size in KB: {kb_size} KB")
print(f"Size in MB: {mb_size} MB")
print(f"Size in GB: {gb_size} GB")

Output:

File size: 1512980 bytes
Size in KB: 1477.51953125 KB
Size in MB: 1.4428901672363281 MB
Size in GB: 0.0014090724289417267 GB

To limit the result to three decimal places, you canĀ use of the round() function in Python.

import os

def get_file_size(file_path):
    # Retrieve the size of the file in bytes
    file_size = os.path.getsize(file_path)

    # Convert file size to kilobytes and round to 3 decimal places
    kb = round(file_size / 1024, 3)

    # Convert kilobytes to megabytes and round to 3 decimal places
    mb = round(kb / 1024, 3)

    # Convert megabytes to gigabytes and round to 3 decimal places
    gb = round(mb / 1024, 3)

    # Create a dictionary to store the file size in different units
    result = {
        "file_size": file_size,
        "kb": kb,
        "mb": mb,
        "gb": gb
    }

    # Return the dictionary with file size information
    return result

New result:

File size: 1512980 bytes
Size in KB: 1477.52 KB
Size in MB: 1.443 MB
Size in GB: 0.001 GB

Conclusion

In conclusion, the os.path.getsize() function in Python's os module allows us to retrieve the size of a file in bytes.

By dividing this file size by the appropriate conversion factors, we can convert it to kilobytes (KB), megabytes (MB), and gigabytes (GB). This enables us to present the file size in a more easily understandable format.

Finally, if you are interested in learning how to convert file sizes using the Hurry library, I encourage you to visit the "Working With Hurry in Python" resource, where you can find detailed information on this topic.