Python — Calculates the total size of a directory and its subdirectories

Python — Calculates the total size of a directory and its subdirectories

If you’re working with files and directories, you’ll likely need to know the total size of a directory and its subdirectories at some point. This can be useful for a variety of tasks, such as determining how much disk space a particular set of files is using or monitoring disk usage on a server. Fortunately, Python provides an easy way to calculate the total size of a directory and its subdirectories using the?os?and?os.path?modules.

In this article, we’ll walk through the steps needed to calculate the total size of a directory and its subdirectories in Python.

Step 1: Import the?os?and?os.path?modules

The?os?module provides a way to interact with the operating system, while the?os.path?module provides functions for working with file paths. To use these modules, you'll need to import them into your Python script:


import os
import os.path        

Step 2: Define a function that calculates the total size of a directory

To calculate the total size of a directory and its subdirectories, we’ll use the?os.walk?method to iterate over all of the directories and files in the specified directory. For each file, we'll check to make sure it is not a symbolic link, and if it is not, we'll add the size of the file to a?total_size?variable.


Here’s the function we’ll use:

def get_dir_size(dir_path):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(dir_path):
        for file in filenames:
            file_path = os.path.join(dirpath, file)
            if not os.path.islink(file_path):
                total_size += os.path.getsize(file_path)
    return total_size        

This function takes a single argument,?dir_path, which is the path to the directory you want to calculate the size of. It returns the total size of the directory in bytes.

Let’s break down how this function works:

  • We initialize a?total_size?variable to 0 to keep track of the total size of the directory.
  • We use the?os.walk?method to iterate over all of the directories and files in the specified directory and its subdirectories. The?os.walk?method returns a tuple for each directory it visits, consisting of the directory path, a list of subdirectory names, and a list of file names.
  • For each file, we check to make sure it is not a symbolic link using the?os.path.islink?method. If it is not a symbolic link, we get the size of the file using the?os.path.getsize?method and add it to the?total_size?variable.
  • Once we have iterated over all of the directories and files, we return the total size of the directory.

Step 3: Call the function with the path to the directory you want to calculate the size of

To use the?get_dir_size?function, you simply need to call it with the path to the directory you want to calculate the size of:


dir_path = "/path/to/directory"
total_size = get_dir_size(dir_path)
print("Total size of directory %s: %s bytes" % (dir_path, total_size))        

In this example, we’ve set the?dir_path?variable to the path of the directory we want to calculate the size of. We then call the?get_dir_size?function with this path, and store the result in the?total_size?variable. Finally, we print out the total size of the directory in bytes using the?print?statement.

Step 4: Display the total size in a human-readable format

While the total size of a directory in bytes can be useful, it can be difficult to interpret without some context. To make it more user-friendly, we can convert the size to a more human-readable format, such as kilobytes, megabytes, or gigabytes.


Here’s a modified version of the?get_dir_size?function that returns the size in a human-readable format:

def get_dir_size(dir_path):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(dir_path):
        for file in filenames:
            file_path = os.path.join(dirpath, file)
            if not os.path.islink(file_path):
                total_size += os.path.getsize(file_path)
    return convert_size(total_size)

def convert_size(size_bytes):
    if size_bytes == 0:
        return "0B"
    size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
    i = int(math.floor(math.log(size_bytes, 1024)))
    p = math.pow(1024, i)
    s = round(size_bytes / p, 2)
    return "%s %s" % (s, size_name[i])        

This modified function includes a?convert_size?helper function that takes the total size in bytes as an argument and converts it to a human-readable format. The?convert_size?function uses logarithms to determine the appropriate suffix for the size (e.g. KB, MB, GB, etc.) and rounds the size to two decimal places.

To use this modified function, simply call it with the path to the directory you want to calculate the size of, as before:

This will print out the total size of the directory in a human-readable format, such as “1.23 MB” or “567.89 GB”.

Conclusion

In this article, we’ve seen how to calculate the total size of a directory and its subdirectories in Python. We’ve used the?os?and?os.path?modules to iterate over all of the files in the specified directory and its subdirectories, and calculated the total size by adding up the size of each file. We've also seen how to convert the size to a more human-readable format using logarithms and suffixes.


By understanding how to calculate the total size of a directory and its subdirectories, you’ll be better equipped to manage your files and directories, monitor disk usage, and perform other useful tasks in Python.

I trust that you found this article beneficial. If you have any comments, feel free to leave them in the comments section below or via?Facebook,?Twitter?or?LinkedIn.

要查看或添加评论,请登录

TechWith Julles的更多文章

社区洞察

其他会员也浏览了