The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Create a file size analyzer for large folders

Here is a Python script that acts as a file size analyzer for large folders. It recursively scans all files in a given directory, calculates their sizes, and provides a summarized output sorted by size.

python
import os from pathlib import Path from collections import defaultdict def human_readable_size(size_bytes): """Convert bytes to human-readable format""" for unit in ['B', 'KB', 'MB', 'GB', 'TB']: if size_bytes < 1024: return f"{size_bytes:.2f} {unit}" size_bytes /= 1024 return f"{size_bytes:.2f} PB" def analyze_folder_size(directory): """Analyzes folder and prints size summary""" folder = Path(directory) if not folder.is_dir(): print(f"Error: {directory} is not a valid directory.") return print(f"nAnalyzing folder: {directory}n") total_size = 0 file_sizes = [] folder_sizes = defaultdict(int) for dirpath, _, filenames in os.walk(folder): for filename in filenames: file_path = Path(dirpath) / filename try: file_size = file_path.stat().st_size total_size += file_size file_sizes.append((str(file_path), file_size)) parent_folder = str(Path(dirpath)) folder_sizes[parent_folder] += file_size except (PermissionError, FileNotFoundError): print(f"Skipped: {file_path}") continue # Sort files by size descending file_sizes.sort(key=lambda x: x[1], reverse=True) folder_sizes_sorted = sorted(folder_sizes.items(), key=lambda x: x[1], reverse=True) print("Top 10 Largest Files:n") for path, size in file_sizes[:10]: print(f"{human_readable_size(size):>12} {path}") print("nTop 10 Largest Folders:n") for folder, size in folder_sizes_sorted[:10]: print(f"{human_readable_size(size):>12} {folder}") print(f"nTotal size of '{directory}': {human_readable_size(total_size)}") if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(description="Analyze file sizes in a folder.") parser.add_argument("path", help="Path to the folder to analyze.") args = parser.parse_args() analyze_folder_size(args.path)

Features:

  • Recursively traverses all files and subfolders.

  • Calculates size of each file and aggregates sizes per folder.

  • Shows top 10 largest files and folders.

  • Displays total size in a human-readable format.

  • Handles permissions and missing files gracefully.

Usage:

Save the script as folder_size_analyzer.py and run from the command line:

bash
python folder_size_analyzer.py /path/to/your/folder

Let me know if you want a GUI version or CSV export functionality.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About