How to get the size of a Directory in Python ?

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
Let’s see how to get the size of a directory in python.
The Python os package gives the flexibility to access the file/directory structure so that we can walk through each file in a directory and get the size of them.
os.walk() function takes the directory path as a parameter and it walks through the directory.
os.path.getsize() function takes the file name as a parameter and gives the size of it.
So the sum of each file size in a directory gives us the total size of the directory.
import os
import sys

if __name__ == '__main__':
    try:
        directory = input("Enter the directory which you want to get the size : ")
    except IndexError:
        sys.exit("Invalid directory")

    dir_size = 0
    readable_sizes = {'B': 1,
                 'KB': float(1) / 1024,
                 'MB': float(1) / (1024 * 1024),
                 'GB': float(1) / (1024 * 1024 * 1024)
                      }
    for (path, dirs, files) in os.walk(directory):
        for file in files:
            filename = os.path.join(path, file)
            dir_size += os.path.getsize(filename)
    readable_sizes_list = [str(round(readable_sizes[key] * dir_size, 2)) + " " + key for key in readable_sizes]

    if dir_size == 0:
        print("File Empty")
    else:
        for units in sorted(readable_sizes_list)[::-1]:
            print("Folder Size: " + units)
Output:
Enter the directory which you want to get the size : C:\Users\cgoka\Desktop\mylibs
Folder Size: 733160003 B
Folder Size: 715976.57 KB
Folder Size: 699.2 MB
Folder Size: 0.68 GB
Python walk()
Happy Learning 🙂

How to check whether a file exists python ?
Python TypeCasting for Different Types
Python How to read input from keyboard
Java 8 walk How to Read all files in a folder
How install Python on Windows 10
How to Search a file in a Directory using Java
Python – How to remove duplicate elements from List
Python – Print different vowels present in a String
Python – Find the biggest of 2 given numbers
How to Remove Spaces from String in Python
How to get Words Count in Python from a File
How to get Characters Count in Python from a File
How to merge two lists in Python
How to Read CSV File in Python
How to Create or Delete Directories in Python ?

How to check whether a file exists python ?
Python TypeCasting for Different Types
Python How to read input from keyboard
Java 8 walk How to Read all files in a folder
How install Python on Windows 10
How to Search a file in a Directory using Java
Python – How to remove duplicate elements from List
Python – Print different vowels present in a String
Python – Find the biggest of 2 given numbers
How to Remove Spaces from String in Python
How to get Words Count in Python from a File
How to get Characters Count in Python from a File
How to merge two lists in Python
How to Read CSV File in Python
How to Create or Delete Directories in Python ?
 


Δ
 Python – Introduction
 Python – Features
 Python – Install on Windows
 Python – Modes of Program
 Python – Number System
 Python – Identifiers
 Python – Operators
 Python – Ternary Operator
 Python – Command Line Arguments
 Python – Keywords
 Python – Data Types
 Python – Upgrade Python PIP
 Python – Virtual Environment
 Pyhton – Type Casting
 Python – String to Int
 Python – Conditional Statements
 Python – if statement
 Python – *args and **kwargs
 Python – Date Formatting
 Python – Read input from keyboard
 Python – raw_input
 Python – List In Depth
 Python – List Comprehension
 Python – Set in Depth
 Python – Dictionary in Depth
 Python – Tuple in Depth
 Python – Stack Datastructure
 Python – Classes and Objects
 Python – Constructors
 Python – Object Introspection
 Python – Inheritance
 Python – Decorators
 Python – Serialization with Pickle
 Python – Exceptions Handling
 Python – User defined Exceptions
 Python – Multiprocessing
 Python – Default function parameters
 Python – Lambdas Functions
 Python – NumPy Library
 Python – MySQL Connector
 Python – MySQL Create Database
 Python – MySQL Read Data
 Python – MySQL Insert Data
 Python – MySQL Update Records
 Python – MySQL Delete Records
 Python – String Case Conversion
 Howto – Find biggest of 2 numbers
 Howto – Remove duplicates from List
 Howto – Convert any Number to Binary
 Howto  – Merge two Lists
 Howto – Merge two dicts
 Howto – Get Characters Count in a File
 Howto – Get Words Count in a File
 Howto – Remove Spaces from String
 Howto –  Read Env variables
 Howto – Read a text File
 Howto – Read a JSON File
 Howto –  Read Config.ini files
 Howto – Iterate Dictionary
 Howto – Convert List Of Objects to CSV
 Howto – Merge two dict in Python
 Howto – create Zip File
 Howto – Get OS info
 Howto – Get size of Directory
 Howto – Check whether a file exists
 Howto –  Remove key from dictionary
 Howto – Sort Objects
 Howto – Create or Delete Directories
 Howto – Read CSV File
 Howto – Create Python Iterable class
 Howto – Access for loop index
 Howto – Clear all elements from List
 Howto – Remove empty lists from a List
 Howto – Remove special characters from String
 Howto – Sort dictionary by key
 Howto – Filter a list