本文目录导读:

这是一个非常实用的想法!我来帮你写一个Python脚本,它可以自动将桌面文件按类型整理到不同的文件夹中。
桌面文件整理脚本
import os
import shutil
from pathlib import Path
def organize_desktop():
"""
自动整理桌面文件,按文件类型分类到不同文件夹
"""
# 获取桌面路径(支持Windows、Mac、Linux)
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
# 定义文件类型分类规则
file_categories = {
"图片": [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".svg", ".webp"],
"文档": [".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".csv"],
"压缩包": [".zip", ".rar", ".7z", ".tar", ".gz"],
"视频": [".mp4", ".avi", ".mkv", ".mov", ".wmv", ".flv"],
"音频": [".mp3", ".wav", ".flac", ".aac", ".ogg"],
"代码": [".py", ".js", ".html", ".css", ".java", ".cpp", ".c", ".h", ".json", ".xml"],
"安装包": [".exe", ".msi", ".dmg", ".pkg", ".deb", ".rpm"],
"其他": [] # 无法识别的文件类型
}
try:
# 遍历桌面文件
for file in os.listdir(desktop_path):
# 跳过文件夹
file_path = os.path.join(desktop_path, file)
if os.path.isdir(file_path):
continue
# 获取文件扩展名(转为小写)
file_ext = os.path.splitext(file)[1].lower()
# 判断文件类型
moved = False
for category, extensions in file_categories.items():
if file_ext in extensions or (category == "其他" and not moved):
# 创建目标文件夹(如果不存在)
target_dir = os.path.join(desktop_path, category)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
print(f"创建文件夹: {category}")
# 移动文件(处理重名问题)
target_path = os.path.join(target_dir, file)
counter = 1
while os.path.exists(target_path):
name, ext = os.path.splitext(file)
target_path = os.path.join(target_dir, f"{name}_{counter}{ext}")
counter += 1
shutil.move(file_path, target_path)
print(f"移动: {file} -> {category}/")
moved = True
break
print("\n桌面整理完成!")
except Exception as e:
print(f"整理过程中出现错误: {e}")
# 运行脚本
if __name__ == "__main__":
organize_desktop()
升级版(带GUI界面)
如果你想要更高级的功能,这里是一个带简单图形界面的版本:
import os
import shutil
import tkinter as tk
from tkinter import messagebox, ttk
from pathlib import Path
import threading
class DesktopOrganizer:
def __init__(self, root):
self.root = root
self.root.title("桌面文件整理工具")
self.root.geometry("600x500")
# 设置界面
self.setup_ui()
# 文件分类规则
self.file_categories = {
"图片": [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".svg", ".webp"],
"文档": [".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".csv"],
"压缩包": [".zip", ".rar", ".7z", ".tar", ".gz"],
"视频": [".mp4", ".avi", ".mkv", ".mov", ".wmv", ".flv"],
"音频": [".mp3", ".wav", ".flac", ".aac", ".ogg"],
"代码": [".py", ".js", ".html", ".css", ".java", ".cpp", ".c", ".h", ".json", ".xml"],
"安装包": [".exe", ".msi", ".dmg", ".pkg", ".deb", ".rpm"],
}
def setup_ui(self):
# 标题
title = tk.Label(self.root, text="桌面文件自动整理", font=("Arial", 16, "bold"))
title.pack(pady=10)
# 说明
desc = tk.Label(self.root, text="将桌面文件按类型分类到不同文件夹", font=("Arial", 10))
desc.pack(pady=5)
# 桌面路径选择
path_frame = tk.Frame(self.root)
path_frame.pack(pady=10)
tk.Label(path_frame, text="目标文件夹:").pack(side=tk.LEFT, padx=5)
self.path_var = tk.StringVar(value=os.path.join(os.path.expanduser("~"), "Desktop"))
path_entry = tk.Entry(path_frame, textvariable=self.path_var, width=40)
path_entry.pack(side=tk.LEFT, padx=5)
# 分类规则显示
rule_frame = tk.LabelFrame(self.root, text="文件分类规则", padx=10, pady=10)
rule_frame.pack(fill="both", expand=True, padx=20, pady=10)
self.rules_text = tk.Text(rule_frame, height=12, width=60)
self.rules_text.pack(fill="both", expand=True)
# 显示分类规则
self.update_rules_display()
# 按钮
button_frame = tk.Frame(self.root)
button_frame.pack(pady=20)
organize_btn = tk.Button(button_frame, text="开始整理", command=self.start_organize,
bg="#4CAF50", fg="white", font=("Arial", 12), padx=20)
organize_btn.pack(side=tk.LEFT, padx=10)
simulate_btn = tk.Button(button_frame, text="模拟整理(预览)", command=self.simulate_organize,
bg="#2196F3", fg="white", font=("Arial", 12), padx=20)
simulate_btn.pack(side=tk.LEFT, padx=10)
# 进度条
self.progress = ttk.Progressbar(self.root, length=400, mode='determinate')
self.progress.pack(pady=10)
# 状态信息
self.status_text = tk.Text(self.root, height=8, width=70)
self.status_text.pack(pady=10)
def update_rules_display(self):
self.rules_text.delete(1.0, tk.END)
for category, extensions in self.file_categories.items():
ext_str = ", ".join(extensions[:5]) # 只显示前5个扩展名
if len(extensions) > 5:
ext_str += "..."
self.rules_text.insert(tk.END, f"{category}: {ext_str}\n")
def log_message(self, message):
self.status_text.insert(tk.END, message + "\n")
self.status_text.see(tk.END)
self.root.update()
def simulate_organize(self):
"""模拟整理,只显示会如何分类而不实际移动文件"""
self.status_text.delete(1.0, tk.END)
self.progress['value'] = 0
desktop_path = self.path_var.get()
if not os.path.exists(desktop_path):
messagebox.showerror("错误", "路径不存在!")
return
self.log_message("开始模拟整理...\n")
files = [f for f in os.listdir(desktop_path) if os.path.isfile(os.path.join(desktop_path, f))]
total = len(files)
for i, file in enumerate(files):
file_ext = os.path.splitext(file)[1].lower()
category = self.get_category(file_ext)
self.log_message(f" {file} -> {category}")
# 更新进度
self.progress['value'] = (i + 1) / total * 100
self.root.update()
self.log_message(f"\n模拟完成!共 {total} 个文件需要整理。")
def get_category(self, file_ext):
for category, extensions in self.file_categories.items():
if file_ext in extensions:
return category
return "其他"
def start_organize(self):
"""开始实际整理文件"""
if not messagebox.askyesno("确认", "确定要开始整理桌面文件吗?"):
return
# 在新线程中运行,避免界面卡顿
thread = threading.Thread(target=self.organize_thread)
thread.start()
def organize_thread(self):
self.status_text.delete(1.0, tk.END)
self.progress['value'] = 0
desktop_path = self.path_var.get()
if not os.path.exists(desktop_path):
self.log_message("错误:路径不存在!")
return
self.log_message("开始整理文件...\n")
# 获取所有文件(不包括文件夹)
files = [f for f in os.listdir(desktop_path) if os.path.isfile(os.path.join(desktop_path, f))]
total = len(files)
moved_count = 0
for i, file in enumerate(files):
file_path = os.path.join(desktop_path, file)
file_ext = os.path.splitext(file)[1].lower()
# 获取目标分类
category = self.get_category(file_ext)
# 创建目标文件夹
target_dir = os.path.join(desktop_path, category)
os.makedirs(target_dir, exist_ok=True)
# 移动文件(处理重名)
target_path = os.path.join(target_dir, file)
counter = 1
original_name = file
while os.path.exists(target_path):
name, ext = os.path.splitext(original_name)
file = f"{name}_{counter}{ext}"
target_path = os.path.join(target_dir, file)
counter += 1
try:
shutil.move(file_path, target_path)
self.log_message(f"✓ {original_name} -> {category}/")
moved_count += 1
except Exception as e:
self.log_message(f"✗ {original_name} 移动失败: {e}")
# 更新进度
self.progress['value'] = (i + 1) / total * 100
self.root.update()
self.log_message(f"\n整理完成!共移动 {moved_count} 个文件。")
messagebox.showinfo("完成", f"桌面整理完成!\n共整理了 {moved_count} 个文件。")
# 运行GUI版本
if __name__ == "__main__":
root = tk.Tk()
app = DesktopOrganizer(root)
root.mainloop()
使用说明
基础版使用:
- 安装Python(如果未安装)
- 将脚本保存为
desktop_organizer.py - 双击运行或
python desktop_organizer.py
GUI版使用:
- 运行脚本后会打开一个图形界面
- 点击"模拟整理"预览结果
- 确认无误后点击"开始整理"
自定义分类规则:
你可以修改 file_categories 字典来添加或修改分类规则,
file_categories = {
"我的文档": [".docx", ".pdf", ".txt"],
"重要图片": [".jpg", ".png"],
# 添加你自己的分类...
}
这个脚本会自动创建新的文件夹,并处理文件名冲突问题,建议先用"模拟整理"功能预览结果,确认没有问题后再进行实际整理!
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播