🔍 PySorta: Deep Dive

Explore the full breakdown of pysorta.py, understand its flow, and contribute with confidence.

📦 Project Structure

PySorta is a single-file script but uses clear modular logic. Here's the flat structure:

pysorta.py README.md deepdive.html assets/

🚀 Script Overview

The script follows a clean top-to-bottom flow:

  1. Import required libraries
  2. Define file category mappings
  3. Get folder from user
  4. Ask how to sort (type/date/size)
  5. Sort using corresponding logic

1️⃣ Imports

import os import shutil from datetime import datetime from pathlib import Path

Why? These are standard Python libraries for working with file systems and timestamps.

2️⃣ File Categories

FILE_CATEGORIES = { "Images": [".jpg", ".jpeg", ".png"], "Documents": [".pdf", ".docx", ".txt"], "3D Models": [".obj", ".glb", ".fbx"] }

This dictionary defines how files are grouped. Add more as needed!

3️⃣ Get Folder Path

folder = input("Enter folder path to organize: ").strip() if not folder: folder = os.getcwd()

If the user doesn’t enter a folder, it uses the current working directory.

4️⃣ Prompt User

print("Choose sorting method:") print("1. By file type") print("2. By date modified") print("3. By file size") choice = input("Enter your choice (1/2/3): ")

Simple CLI interface to choose sorting mode.

5️⃣ Sorting Functions

def sort_by_type(path): for file in path.iterdir(): if file.is_file(): for category, extensions in FILE_CATEGORIES.items(): if file.suffix.lower() in extensions: target_folder = path / category target_folder.mkdir(exist_ok=True) shutil.move(str(file), str(target_folder / file.name))

This is the core logic for organizing by type. The other two modes (date/size) use similar logic.

More clarity coming as I develop this walkthrough page, thanks for checking it out!