本文目录导读:

我来为您介绍几种桌面整理脚本的编写方法,从简单到进阶,您可以根据需求选择:
Python 桌面整理脚本(最常用)
基础版本:按文件类型分类
import os
import shutil
from pathlib import Path
def organize_desktop():
# 获取桌面路径
desktop = Path.home() / 'Desktop'
# 定义文件分类规则
categories = {
'图片': ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg'],
'文档': ['.doc', '.docx', '.txt', '.pdf', '.xls', '.xlsx', '.ppt', '.pptx'],
'视频': ['.mp4', '.avi', '.mkv', '.mov', '.wmv'],
'音乐': ['.mp3', '.wav', '.flac', '.aac'],
'压缩包': ['.zip', '.rar', '.7z', '.tar', '.gz'],
'程序': ['.exe', '.msi', '.app', '.sh'],
'代码': ['.py', '.js', '.java', '.html', '.css', '.cpp']
}
# 创建分类文件夹并移动文件
for file in desktop.iterdir():
if file.is_file(): # 只处理文件,不处理文件夹
moved = False
for category, extensions in categories.items():
if file.suffix.lower() in extensions:
dest_dir = desktop / category
dest_dir.mkdir(exist_ok=True)
shutil.move(str(file), str(dest_dir / file.name))
print(f"移动 {file.name} -> {category}/")
moved = True
break
# 未分类的文件放入"其他"文件夹
if not moved:
other_dir = desktop / '其他'
other_dir.mkdir(exist_ok=True)
shutil.move(str(file), str(other_dir / file.name))
print(f"移动 {file.name} -> 其他/")
if __name__ == "__main__":
organize_desktop()
print("桌面整理完成!")
进阶版本:带GUI界面和高级功能
import tkinter as tk
from tkinter import filedialog, messagebox
import os
import shutil
from datetime import datetime
import threading
class DesktopOrganizer:
def __init__(self):
self.root = tk.Tk()
self.root.title("桌面整理工具")
self.root.geometry("500x400")
self.setup_ui()
def setup_ui(self):
# 标题
title = tk.Label(self.root, text="桌面整理助手", font=("Arial", 16))
title.pack(pady=20)
# 说明文字
info = tk.Label(self.root, text="选择整理方式:", font=("Arial", 12))
info.pack(pady=10)
# 整理选项
self.organize_type = tk.StringVar(value="type")
type_option = tk.Radiobutton(self.root, text="按文件类型整理",
variable=self.organize_type, value="type")
type_option.pack()
date_option = tk.Radiobutton(self.root, text="按日期整理",
variable=self.organize_type, value="date")
date_option.pack()
# 整理按钮
btn = tk.Button(self.root, text="开始整理", command=self.start_organize,
bg="#4CAF50", fg="white", font=("Arial", 12))
btn.pack(pady=20)
# 状态标签
self.status = tk.Label(self.root, text="就绪", font=("Arial", 10))
self.status.pack()
def start_organize(self):
# 在新线程中执行整理,避免界面卡顿
threading.Thread(target=self.organize, daemon=True).start()
def organize(self):
desktop = os.path.expanduser("~/Desktop")
# 更新状态
self.root.after(0, lambda: self.status.config(text="正在整理..."))
# 获取所有文件
files = [f for f in os.listdir(desktop)
if os.path.isfile(os.path.join(desktop, f))]
for file in files:
file_path = os.path.join(desktop, file)
try:
if self.organize_type.get() == "type":
self.organize_by_type(file_path, desktop)
else:
self.organize_by_date(file_path, desktop)
except Exception as e:
print(f"处理文件 {file} 时出错: {e}")
self.root.after(0, lambda: self.status.config(text="整理完成!"))
messagebox.showinfo("完成", "桌面整理完成!")
def organize_by_type(self, file_path, desktop):
# 获取文件扩展名
extension = os.path.splitext(file_path)[1]
if not extension:
return
# 创建文件夹并移动
folder_name = extension[1:].upper() + "文件" # PDF文件
dest_folder = os.path.join(desktop, folder_name)
os.makedirs(dest_folder, exist_ok=True)
shutil.move(file_path, os.path.join(dest_folder, os.path.basename(file_path)))
def organize_by_date(self, file_path, desktop):
# 获取文件修改时间
timestamp = os.path.getmtime(file_path)
date = datetime.fromtimestamp(timestamp)
# 按月创建文件夹
folder_name = date.strftime("%Y年%m月")
dest_folder = os.path.join(desktop, folder_name)
os.makedirs(dest_folder, exist_ok=True)
shutil.move(file_path, os.path.join(dest_folder, os.path.basename(file_path)))
def run(self):
self.root.mainloop()
if __name__ == "__main__":
app = DesktopOrganizer()
app.run()
PowerShell 桌面整理脚本(Windows)
# organize-desktop.ps1
param(
[string]$DesktopPath = "$env:USERPROFILE\Desktop"
)
function Organize-Desktop {
Write-Host "开始整理桌面..." -ForegroundColor Green
# 定义文件分类
$categories = @{
'Images' = @('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg')
'Documents' = @('.doc', '.docx', '.txt', '.pdf', '.xls', '.xlsx', '.ppt', '.pptx')
'Videos' = @('.mp4', '.avi', '.mkv', '.mov')
'Audio' = @('.mp3', '.wav', '.flac')
'Archives' = @('.zip', '.rar', '.7z', '.tar', '.gz')
'Programs' = @('.exe', '.msi', '.bat')
'Code' = @('.py', '.js', '.html', '.css', '.cpp', '.java')
}
# 遍历桌面文件
Get-ChildItem -Path $DesktopPath -File | ForEach-Object {
$file = $_
$moved = $false
foreach ($category in $categories.Keys) {
if ($file.Extension.ToLower() -in $categories[$category]) {
$destDir = Join-Path $DesktopPath $category
if (!(Test-Path $destDir)) {
New-Item -ItemType Directory -Path $destDir | Out-Null
}
Move-Item $file.FullName -Destination $destDir
Write-Host "移动: $($file.Name) -> $category/" -ForegroundColor Yellow
$moved = $true
break
}
}
# 未分类文件
if (!$moved) {
$otherDir = Join-Path $DesktopPath "其他"
if (!(Test-Path $otherDir)) {
New-Item -ItemType Directory -Path $otherDir | Out-Null
}
Move-Item $file.FullName -Destination $otherDir
Write-Host "移动: $($file.Name) -> 其他/" -ForegroundColor Yellow
}
}
Write-Host "桌面整理完成!" -ForegroundColor Green
}
# 运行整理
Organize-Desktop
Bash 脚本(Linux/Mac)
#!/bin/bash
# organize-desktop.sh
# 设置桌面路径
DESKTOP="$HOME/Desktop"
# 创建分类函数
organize_folder() {
local ext="$1"
local folder="$2"
if [ -d "$DESKTOP" ]; then
# 创建文件夹(如果不存在)
mkdir -p "$DESKTOP/$folder"
# 查找并移动匹配文件
find "$DESKTOP" -maxdepth 1 -type f \( -name "*.$ext" \) -exec mv {} "$DESKTOP/$folder/" \;
fi
}
echo "开始整理桌面..."
# 分类整理
organize_folder "jpg" "图片"
organize_folder "jpeg" "图片"
organize_folder "png" "图片"
organize_folder "gif" "图片"
organize_folder "pdf" "文档"
organize_folder "doc" "文档"
organize_folder "docx" "文档"
organize_folder "txt" "文档"
organize_folder "mp4" "视频"
organize_folder "avi" "视频"
organize_folder "mkv" "视频"
organize_folder "mp3" "音乐"
organize_folder "wav" "音乐"
organize_folder "zip" "压缩包"
organize_folder "rar" "压缩包"
# 移动其他未分类的 .py 文件和代码文件
organize_folder "py" "代码"
organize_folder "js" "代码"
organize_folder "html" "代码"
# 处理其他文件
mkdir -p "$DESKTOP/其他"
find "$DESKTOP" -maxdepth 1 -type f ! -name ".*" | while read file; do
folder=$(basename "$file")
mv "$file" "$DESKTOP/其他/"
done
echo "桌面整理完成!"
使用方法
Windows PowerShell:
# 执行脚本 powershell -ExecutionPolicy Bypass -File organize-desktop.ps1
Linux/Mac:
# 给脚本执行权限 chmod +x organize-desktop.sh # 运行脚本 ./organize-desktop.sh
Python:
# 安装所需依赖 pip install tkinter # 如果需要GUI # 运行脚本 python organize_desktop.py
高级功能建议
如果您需要更多功能,可以考虑添加:
- 自动运行(通过任务计划程序或cron)
- 文件去重(检查重复文件)
- 预览功能(整理前预览移动操作)
- 撤销功能(记录操作日志)
- 自定义规则(允许用户配置分类规则)
您可以根据实际需求选择或修改这些脚本,如果需要特定功能的实现,请告诉我具体需求。
版权声明: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直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播