Open In App

How to get size of folder using Python?

Last Updated : 21 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to discuss various approaches to get the size of a folder using python.

To get the size of a directory, the user has to walk through the whole folder and add the size of each file present in that folder and will show the total size of the folder. 

Steps to be followed:

  • Import required module.
  • Create a variable size and assign 0 to it.
  • Assign path of the folder.
  • Scan the folder and get the size of each file in the folder and add it to size.
  • Display the total size of the folder.

We are going to use the below folder to depict each approach:

Method #1: Using os.walk() + os.path.getsize()

In this approach, we will iterate each file present in the folder using os.walk() and then compute and add the size of each scanned file using os.path.getsize().

Python3




# import module
import os
 
# assign size
size = 0
 
# assign folder path
Folderpath = 'C:/Users/Geetansh Sahni/Documents/R'
 
# get size
for path, dirs, files in os.walk(Folderpath):
    for f in files:
        fp = os.path.join(path, f)
        size += os.path.getsize(fp)
 
# display size
print("Folder size: " + str(size))


Output:

Method #2: Using os.walk() + os.stat()

In this approach, we will iterate each file present in the folder using os.walk(). The os.stat() method returns file properties, now using os.stat().st_size we can compute the file size. Hence, the total folder size can be calculated.

Python3




# import module
import os
 
# assign size
size = 0
 
# assign folder path
Folderpath = 'C:/Users/Geetansh Sahni/Documents/R'  
 
# get size
for path, dirs, files in os.walk(Folderpath):
    for f in files:
        fp = os.path.join(path, f)
        size += os.stat(fp).st_size
 
# display size       
print("Folder size: " + str(size))


Output:

Method #3: Using os.scandir() + os.path.getsize()

Here, we will scan each file present in the folder using os.scandir() and then we will compute each file size using os.path.getsize(), then we will store the total added size in a variable.

Python3




# import module
import os
 
# assign size
size = 0
 
# assign folder path
Folderpath = 'C:/Users/Geetansh Sahni/Documents/R'   
 
# get size
for ele in os.scandir(Folderpath):
    size+=os.path.getsize(ele)
     
print(size)


Output:

Method #4: Using os.scandir() + os.stat()

Here, we will scan each file present in the folder using os.scandir(). The os.stat() method returns file properties, now using os.stat().st_size we can compute the file size. Hence, the total folder size can be calculated.

Python3




# import module
import os
 
# assign size
size = 0
 
# assign folder path
Folderpath = 'C:/Users/Geetansh Sahni/Documents/R'  
 
# get size
for ele in os.scandir(Folderpath):
    size+=os.stat(ele).st_size
     
print(size)


Output:



Previous Article
Next Article

Similar Reads

How to merge multiple folders into one folder using Python ?
In this article, we will discuss how to move multiple folders into one folder. This can be done using Python's OS and Shutil module. Approach:Get the current directory and the list of the folders you want to merge.Loop through the list of folders and store their content in a list. Here, we have stored them in the dictionary so that we can have the
4 min read
How to Merge all excel files in a folder using Python?
In this article, we will see how to combine all Excel files present in a folder into a single file. Module used: The python libraries used are: Pandas: Pandas is a python library developed for a python programming language for manipulating data and analyzing the data. It is widely used in Data Science and Data analytics.Glob: The glob module matche
3 min read
Rename a folder of images using Tkinter
Prerequisites: Python GUI – tkinter, os.listdir() method, os.rename() method In this article, the task is to rename a batch of images using Python. Now say given n images in a folder having random names. For example, consider the images below. Now the requirement is to rename them in ordered fashion and appending their original dimensions of respec
3 min read
Python - Move all files from subfolders to main folder
This article will discuss how to move all files from the subfolder to the main folder using Python. The approach is simple it is similar to moving files from one folder to another using Python, except here the main folder or parent folder of the subfolder is passed as the destination. Modules UsedOS Module: OS module in Python provides functions to
3 min read
How to read multiple text files from folder in Python?
Prerequisite: File Handlingos Python is a strong language which is extremely capable even when it comes to file handling. In this article, we will learn how to read multiple text files from a folder using python. Approach: Import modulesAdd path of the folderChange directoryGet the list of a file from a folderIterate through the file list and check
2 min read
How to run multiple Python file in a folder one after another?
In this article, we will discuss how to run multiple python files in a folder one after another. There can be many ways to this task, here, we will discuss a few of them. For doing this program, we have to create some python files in one folder and give some names to that folder. The content inside a.py file: print("a") The content inside b.py file
3 min read
Import Modules From Another Folder in Python
In this article, we are going to see how to import a module from another folder, While working on big projects we may confront a situation where we want to import a module from a different directory, here we will see the different ways to import a module form different folder. It can be done in two ways: Using sys.pathUsing PythonPath. Create a mod
2 min read
Python - Copy Files From Subfolders to the Main folder
In this article, we will discuss how to copy a file from the subfolder to the main folder. The directory tree that will be used for explanation in this article is as shown below: D:\projects\base | |__subfolder: | \__file.txt | |__main.py | Here we have a folder named "base" in which we have a folder named "subfolder" which contains a file name fil
4 min read
How to iterate through images in a folder Python?
In this article, we will learn how to iterate through images in a folder in Python. Method 1: Using os.listdirExample 1: Iterating through .png onlyAt first we imported the os module to interact with the operating system.Then we import listdir() function from os to get access to the folders given in quotes.Then with the help of os.listdir() functio
2 min read
Automatically organize downloads folder in Python
In this article, we are going to know how to automatically organize the download folder in Python. Are you a disorganized person who just keeps on downloading the files and does not organize them? Then, you must definitely check out this article as in this article, we will be discussing how to automatically organize the downloads folder in Python.
6 min read