Skip to content

Instantly share code, notes, and snippets.

@esauOp
Created November 11, 2025 19:27
Show Gist options
  • Select an option

  • Save esauOp/b16b7f52f89a1507c53209b5762431e6 to your computer and use it in GitHub Desktop.

Select an option

Save esauOp/b16b7f52f89a1507c53209b5762431e6 to your computer and use it in GitHub Desktop.
A tiny sorter that files by type automatically
#!/usr/bin/env python3
from pathlib import Path
import shutil
DOWNLOADS = Path.home() / "Downloads"
BUCKETS = {
"Documents": {".pdf", ".docx", ".doc", ".txt", ".md", ".odt", ".rtf"},
"Images": {".png", ".jpg", ".jpeg", ".gif", ".webp", ".svg", ".heic"},
"Audio": {".mp3", ".wav", ".flac", ".m4a", ".ogg"},
"Video": {".mp4", ".mkv", ".mov", ".avi", ".webm"},
"Archives": {".zip", ".tar", ".gz", ".bz2", ".7z", ".xz"},
"Code": {".py", ".js", ".ts", ".sh", ".rb", ".go", ".c", ".cpp", ".java"},
"Spreadsheets": {".csv", ".xlsx", ".xls", ".ods"},
"Installers": {".deb", ".rpm", ".msi", ".exe", ".dmg", ".pkg", ".apk"},
"Other": set(),
}
def bucket_for(path: Path) -> Path:
ext = path.suffix.lower()
for folder, exts in BUCKETS.items():
if ext in exts:
return DOWNLOADS / folder
return DOWNLOADS / "Other"
def main():
for item in DOWNLOADS.iterdir():
if item.is_file():
target_dir = bucket_for(item)
target_dir.mkdir(exist_ok=True)
try:
shutil.move(str(item), str(target_dir / item.name))
except Exception:
# Skip files that cannot be moved
continue
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment