找回密码
 立即注册
搜索
热搜: STM32
查看: 39|回复: 0

Python Auto File Organizer - Organize Downloads in 10 Min

[复制链接]

28

主题

0

回帖

102

积分

注册会员

积分
102
发表于 2026-3-22 17:03:03 |北京| 显示全部楼层 |阅读模式
Quick Start: Organize Files in 10 Minutes

What You Need:
- Python 3.6+
- No extra libraries needed (built-in modules only)

The Problem: Your Downloads folder is a mess. Let Python fix it automatically.

Complete Code:

import os, shutil
from pathlib import Path

def organize_folder(folder_path):
    categories = {
        'Images': ['.jpg','.jpeg','.png','.gif','.bmp','.svg','.webp'],
        'Documents': ['.pdf','.doc','.docx','.txt','.md','.csv','.xlsx'],
        'Videos': ['.mp4','.avi','.mkv','.mov','.wmv'],
        'Audio': ['.mp3','.wav','.flac','.aac','.ogg'],
        'Archives': ['.zip','.rar','.7z','.tar','.gz'],
        'Code': ['.py','.js','.html','.css','.java','.cpp','.c'],
        'Programs': ['.exe','.msi','.deb','.rpm','.app'],
    }
   
    stats = {'moved': 0, 'errors': 0}
   
    for filename in os.listdir(folder_path):
        filepath = os.path.join(folder_path, filename)
        if not os.path.isfile(filepath):
            continue
        ext = Path(filename).suffix.lower()
        target_folder = 'Other'
        for category, extensions in categories.items():
            if ext in extensions:
                target_folder = category
                break
        dest = os.path.join(folder_path, target_folder)
        os.makedirs(dest, exist_ok=True)
        try:
            shutil.move(filepath, os.path.join(dest, filename))
            print(f"Moved: {filename} -> {target_folder}/")
            stats['moved'] += 1
        except Exception as e:
            print(f"Error: {filename} - {e}")
            stats['errors'] += 1
   
    print(f"Done! Moved {stats['moved']} files.")

if __name__ == '__main__':
    target = input("Enter folder path: ").strip()
    if os.path.isdir(target):
        organize_folder(target)

How It Works:
1. Scans all files in target folder
2. Checks file extension against category list
3. Creates subfolder for each category
4. Moves files into matching folders

Usage: python3 file_organizer.py

Result:
Before: Downloads/ (100+ mixed files)
After:  Downloads/
        - Images/     (45 files)
        - Documents/  (23 files)
        - Videos/     (12 files)
        - Audio/      (8 files)
        - Archives/   (6 files)
        - Code/       (4 files)
        - Other/      (2 files)

Bonus: Add to crontab for daily auto-organize:
0 0 * * * python3 /path/to/file_organizer.py /home/user/Downloads

Key Features:
- Pure Python, no pip install needed
- Cross-platform (Windows/Mac/Linux)
- Error handling included
- Auto creates folders

Save time. Automate boring tasks. Happy coding!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Discuz! X

GMT+8, 2026-4-5 14:58 , Processed in 0.029023 second(s), 20 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表