自动化开发工具集

覆盖文件批处理、定时任务、通知推送三大高频场景的 Python 自动化脚本,可直接复制改造。

场景:日常开发自动化 技术栈:Python / schedule / smtplib 来源:GitHub 参考

项目简介

自动化是提效的第一杠杆。这里提供三个可独立运行的 Python 脚本:批量文件整理(按扩展名归档)、定时任务调度(schedule 库)、失败邮件通知(smtplib)。组合使用即可搭建自己的开发工作流。

代码示例:批量文件整理

py
import shutil
from pathlib import Path

RULES = {
    ".jpg": "Images", ".png": "Images", ".gif": "Images",
    ".pdf": "Docs", ".docx": "Docs", ".txt": "Docs",
    ".py": "Code", ".js": "Code", ".html": "Code",
    ".zip": "Archives", ".rar": "Archives",
}

def organize(directory: str) -> int:
    src = Path(directory)
    moved = 0
    for f in src.iterdir():
        if not f.is_file():
            continue
        target_dir = src / RULES.get(f.suffix.lower(), "Others")
        target_dir.mkdir(exist_ok=True)
        shutil.move(str(f), str(target_dir / f.name))
        moved += 1
    return moved

if __name__ == "__main__":
    print("moved", organize("C:/Downloads"), "files")

代码示例:定时任务 + 邮件通知

py
import schedule
import smtplib
from email.mime.text import MIMEText

def send_notify(subject: str, body: str) -> None:
    msg = MIMEText(body, "plain", "utf-8")
    msg["Subject"] = subject
    msg["From"] = "bot@example.com"
    msg["To"] = "me@example.com"
    with smtplib.SMTP("smtp.example.com", 587) as s:
        s.starttls()
        s.login("bot@example.com", "PASSWORD")  # 请使用环境变量注入
        s.send_message(msg)

def backup_job():
    n = organize("C:/Downloads")
    send_notify("下载目录已整理", f"共移动 {n} 个文件")

schedule.every().day.at("09:00").do(backup_job)
while True:
    schedule.run_pending()

操作步骤

  1. RULES 扩展名改为你的实际需求,修改目录路径后直接运行 organize()
  2. 定时任务用 pip install schedule 后即可运行;Windows 场景建议改用任务计划程序。
  3. 邮件密码不要写死在代码里,建议用环境变量或密钥管理服务注入。

来源参考

GitHub 关键词:python-auto-organizeschedule-pythondev-workflow-automation(链接可替换为实际仓库地址)。