How to check the size of a file using Python?

Posted in /  

How to check the size of a file using Python?
vinaykhatri

Vinay Khatri
Last updated on December 23, 2024

    Every file on the computer occupies some space in the memory, although you can just right-click on the file and check its disk size by looking at its properties. But it would be cool if you knew how to calculate the file size using Python code.

    In this Python tutorial, you will learn how to check the size of a file in Python using Python standard Path module. Before we dive into the program, let's discuss the Python module that we will be using in this tutorial.

    How to check the size of a file in Python

    Required module

    Python pathlib module

    As the module name suggests, Python pathlib is a filesystem module that can work with different file systems irrespective of the operating system. I

    n this tutorial, we will use pathlib Path class to find the size states of a file. And using logic, we will convert that size into a human-readable bytes format. The pathlib module comes under the Python standard module suits, so we do not need to install it separately. Now we are all set with our required module, let's open your best Python IDE or text editor and start coding along.

    Python program to find the file size.

    Let's begin with importing the Path class from pathlib module.

    from pathlib import Path

    Now let's define a Python function byte_converter() that accepts file size in bytes and converts it into corresponding KB, MB, GB, or TB.

    def byte_converter(size):
        power = 2**10
        n = 0
        size_format = {0 : '', 1: 'KB', 2: 'MB', 3: 'GB', 4: 'TB'}
        while size > power:
            size /= power
            n += 1
        return str(round(size,2)) +" "+ size_format[n]

    The above code is the basic logic of the byte converter into readable size formats. Now let's define the find_size function that will find the size of the file.

    def find_size(file):
        file_stat =Path(file).stat()
    
        print(byte_converter(file_stat.st_size))

    The Path(file).stat() function will return an object containing different properties of the file. The st_size property will return the file size in bytes. The byte_converter() function will convert the file bytes size into a more readable size format. Now let's define the file Path and which size we want to find.

    file =r"C:\Users\tsmehra\Videos\Captures\video.mp4"

    As you can see that I have specified the file path as a raw string by putting the r prefix before the string. The raw string makes sure that \ symbol is treated as a character instead of an escape sequence. Now call the function find_size() and pass the file as a parameter. Put all the code together and execute.

    #Python program to find the size of a file

    from pathlib import Path
    
    def byte_converter(size):
        power = 2**10
        n = 0
        size_format = {0 : '', 1: 'KB', 2: 'MB', 3: 'GB', 4: 'TB'}
        while size > power:
            size /= power
            n += 1
        return str(round(size,2)) +" "+ size_format[n]
    
    def find_size(file):
        file_stat =Path(file).stat()
        print(f"The size of the file {file} is: ",)
        print(byte_converter(file_stat.st_size))
    
    
    file =r"C:\Users\tsmehra\Videos\Captures\video.mp4"
    
    find_size(file)

    Output

    The size of the file C:\Users\tsmehra\Videos\Captures\video.mp4 is: 
    1.06 GB

    Conclusion

    In this Python tutorial, we discussed how we could use the Python standard pathlib module to find the size of a file. Using the pathlib module, we can do many file management tasks like finding the file creation time, uid, gid, alter time, etc.

    If you want to know more about Python pathlib module, please check out its official documentation .

    People are also reading:

    Leave a Comment on this Post

    0 Comments