How To Get File Size In Python

In this blog post, we’ll learn how to get the size of a file in Python using the os module. The os module provides a function called os.path.getsize(), which returns the size of a file in bytes.

Getting File Size using os.path.getsize()

To get the size of a file in Python, you can use the os.path.getsize() function. You need to pass the file path as a parameter to this function. Here’s an example:

    import os

    file_path = "example.txt"
    size_bytes = os.path.getsize(file_path)
    print(f"Size of {file_path}: {size_bytes} bytes")
    

In this example, we first import the os module. Then, we define the path of the file whose size we want to find. We pass this file path to the os.path.getsize() function, which returns the size of the file in bytes. Finally, we print the file size.

Converting File Size to Human-Readable Format

The file size returned by os.path.getsize() is in bytes. It might be more useful to display the file size in a human-readable format, such as kilobytes (KB), megabytes (MB), or gigabytes (GB). Here’s a function to convert the file size to a human-readable format:

    def convert_size(size_bytes):
        if size_bytes == 0:
            return "0B"
        
        size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
        index = int(math.floor(math.log(size_bytes, 1024)))
        power = math.pow(1024, index)
        size = round(size_bytes / power, 2)
        return f"{size} {size_name[index]}"
    

The convert_size() function takes the file size in bytes as an input and returns a string with a human-readable file size. It uses the math module to calculate the file size in the appropriate unit.

Example: Get File Size and Convert to Human-Readable Format

Let’s combine the above concepts and create a program that gets the file size and converts it to a human-readable format. Here’s the complete code:

    import os
    import math

    def convert_size(size_bytes):
        if size_bytes == 0:
            return "0B"
        
        size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
        index = int(math.floor(math.log(size_bytes, 1024)))
        power = math.pow(1024, index)
        size = round(size_bytes / power, 2)
        return f"{size} {size_name[index]}"

    file_path = "example.txt"
    size_bytes = os.path.getsize(file_path)
    size_human_readable = convert_size(size_bytes)
    print(f"Size of {file_path}: {size_human_readable}")
    

In this example, we first import the required modules and define the convert_size() function. Then, we define the file path and use the os.path.getsize() function to get the file size in bytes. We pass the file size to the convert_size() function, which returns the file size in a human-readable format. Finally, we print the file size in the human-readable format.

Conclusion

In this blog post, we learned how to get the size of a file in Python using the os.path.getsize() function. We also looked at how to convert the file size to a human-readable format. With this knowledge, you can easily get and display file sizes in your Python projects.