How to Calculate a Directory Size Using Python?Introduction:In this tutorial we are learning about how to calculate a directory size using Python. A directory is defined as a collection of subdirectories and files. These subdirectories are separated in the directory hierarchy by using the "/" operator. Directory hierarchies are created by organizing all files and subdirectories into a main directory (also known as the "root" directory). When we want to calculate the size of a directory, we treat it as the root directory and calculate the individual sizes of all files and subdirectories (if any) contained in it. We need to follow a few steps to calculate a directory size using Python, which is given below -
So, to get the size of the directory, we need to traverse the hierarchy to get the size of all files in it. Python offers several ways to do this, which are listed below -
Now, we learn about the above methods for calculating a directory size using Python. 1. Using os.path.getsize() method:The os.path.getsize() method is used to get the size of a file in a directory. We can add the sizes of all files present in the list to get the total size of the list. However, in addition to this method, we also use the os.walk() method to navigate through all the files in the directory. The os.path.getsize() method accepts the path of the file as an argument. Then, it returns the file size in bytes. Program Code: Here, we give a program code for calculating a directory size using the os.path.getsize() method in Python. Here, we use a loop to traverse the directory hierarchy with the help of the os.walk() method. Then use the os.path.join() method to get the path of each file stored in this directory and pass it as a parameter to the os.path.getsize() method. Then, add and view the size of all files. The code is given below - Output: Now, we run the above code and find the size of the directory. It should be noted that for different directories the output will be different. The output is given below - x The directory size is: 2768215 Besides the os.walk() method, we can also use the os.scandir() method or the os.listdir() method to list the saved files and store their size. Program Code: Here, we give a program code for calculating a directory size using the scandir() method in Python. We use the scandir() method to scan the current directory and recursively get the sizes of all files currently in it. Add the size together to maintain the total size of the directory. The code is given below - Output: Now, we run the above code and find the size of the directory. The output is given below - The directory size is: 2278601 Program Code: Here, we give a program code for calculating a directory size using the os.listdir() method in Python. We use the os.listdir() method in place of the os.scandir() method. The code is given below - Output: Now, we run the above code and find the size of the directory. The output is given below - The directory size is: 2287463 2. Using os.stat().st_size property:Another way to calculate the file size is to use the os.stat().st_size property. The os.stat() method is used to get the size of the file (in bytes) or other relevant information. Since we only need the size of the file, we use the st_size property. Program Code: Here, we give a program code for calculating a directory size using the os.stat().st_size property in Python. In this below program code, we import the pathlib module and use the glob() method to list all files located in the current directory. Then, if the files exist in the directory, their size is recalculated using the os.stat().st_size property. The code is given below - Output: Now, we run the above code and find the size of the directory. The output is given below - The directory size is: 2279832 Program Code: Here, we give a program code for calculating a directory size using the os.stat().st_size property in Python. We can also use the os.scandir() method instead of the glob() method to list all files in a directory. The code is given below - Output: Now, we run the above code and find the size of the directory. The output is given below - The directory size is: 2771236 3. Using du command in *NIX OSes:Another way to calculate the file size is to use the du command in NIX OSes. If you are using a NIX operating system, you can call the du command using the subprocess module, which is easier than the above method. Program Code: Here, we give a program code for calculating a directory size using the du command in *NIX OSes in Python. The code is given below - Output: Now, we run the above code and find the size of the directory. The output is given below - The directory size is: 9.0K Conclusion:In this tutorial, we learn how to calculate a directory size using Python. We learn various methods for calculating a directory size and some program code in these methods. Next TopicHow to dynamically import python module |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India