diff --git a/Python/Folder size calculator/README.md b/Python/Folder size calculator/README.md new file mode 100644 index 0000000..6ab3f2e --- /dev/null +++ b/Python/Folder size calculator/README.md @@ -0,0 +1,23 @@ +A simple Python CLI tool that calculates the total size of a folder along with the size of the files and subfolders within it. + +Features: +- Calculates total folder and subfolder sizes recursively. +- Displays sizes of both subfolders and individual files. +- Outputs total size of the entire directory. +- Displays sizes in human-readable format (KB, MB, GB). + +Usage: +'''bash +python folder_size_calculator.py + +Example usage: +python folder_size_calculator.py "D:\Games" + +Output: + +Calculating folder sizes inside: D:\Games + +Game1/ = 2.34 GB +Game2/ = 512.43 MB +notes.txt = 12.40 KB +Total size of 'D:\Games': 2.85 GB diff --git a/Python/Folder size calculator/folder_size_calculator.py b/Python/Folder size calculator/folder_size_calculator.py new file mode 100644 index 0000000..fb04e49 --- /dev/null +++ b/Python/Folder size calculator/folder_size_calculator.py @@ -0,0 +1,65 @@ +import os +import sys + +def get_folder_size(path): + total_size = 0 + # Walk through all directories and files inside the given path + for dirpath, dirnames, filenames in os.walk(path): + for f in filenames: + fp = os.path.join(dirpath, f) + # Check if file exists and is not a symbolic link + if not os.path.islink(fp) and os.path.exists(fp): + try: + total_size += os.path.getsize(fp) # Add file size (in bytes) + except (PermissionError, FileNotFoundError): + pass + return total_size + + +def format_size(size_in_bytes): + for unit in ['B', 'KB', 'MB', 'GB', 'TB']: + if size_in_bytes < 1024: + # Return formatted size when it fits the current unit + return f"{size_in_bytes:.2f} {unit}" + size_in_bytes /= 1024 + return f"{size_in_bytes:.2f} PB" + + +def main(): + # Ensure a folder path is provided as command-line argument + if len(sys.argv) < 2: + print("Usage: python folder_size_calculator.py ") + sys.exit(1) + + folder_path = sys.argv[1] # Read the folder path from arguments + + if not os.path.exists(folder_path): + print(f"Error: The path '{folder_path}' does not exist.") + sys.exit(1) + + print(f"\nCalculating folder sizes inside: {folder_path}\n") + + total_size = 0 # Variable to store total folder size + + # Loop through everything inside the given folder + for item in os.listdir(folder_path): + item_path = os.path.join(folder_path, item) + if os.path.isdir(item_path): + size = get_folder_size(item_path) + print(f"{item}/".ljust(45) + f"= {format_size(size)}") + total_size += size + + # If the item is a file — get its direct size + elif os.path.isfile(item_path): + try: + size = os.path.getsize(item_path) + print(f"{item}".ljust(45) + f"= {format_size(size)}") + total_size += size + except (PermissionError, FileNotFoundError): + pass + + # Finally, print total folder size + print(f"Total size of '{folder_path}': {format_size(total_size)}") + +if __name__ == "__main__": + main()