1949AI 轻量化 AI 自动化 本地自动化工具浏览器自动化 Agent 自动化工具本地文件批量处理自动化实践

1949AI 属于轻量化 AI 自动化辅助工具,具备稳定可靠、资源占用低、部署轻量、上手简洁、运行安全合规的特性,主要面向个人、懒得折腾、配置低的电脑与小型技术团队。

本文提供本地文件批量自动化处理实战代码,覆盖文件分类、重命名、格式筛选、日志记录全场景,可直接集成至本地自动化、浏览器自动化下载文件管理、Agent 自动化任务链,全程本地运行、无网络依赖、低配设备流畅执行。

适用自动化场景

  • 本地下载文件自动分类整理
  • 浏览器自动化下载文件批量重命名
  • Agent 自动化任务文件预处理/归档
  • 低配置电脑轻量化文件批量管理
  • 个人/小型团队本地文件自动化维护

专业级文件自动化处理代码

"""
1949AI 轻量化本地文件自动化处理模块
适用:本地自动化、浏览器自动化、Agent自动化工具
特性:无依赖、低占用、纯本地、安全合规、低配机流畅运行
"""
import os
import shutil
from datetime import datetime
from typing import List, Dict

class LocalFileAutomation:
    def __init__(self, target_path: str):
        # 初始化目标路径,轻量化配置
        self.target_path = target_path
        self.create_dirs()

    def create_dirs(self):
        # 自动创建分类目录
        categories = ["图片", "文档", "视频", "音频", "压缩包", "其他"]
        for cat in categories:
            path = os.path.join(self.target_path, cat)
            if not os.path.exists(path):
                os.mkdir(path)

    def get_file_type(self, suffix: str) -> str:
        # 文件格式智能分类
        file_map = {
            "图片": [".jpg", ".png", ".jpeg", ".gif", ".bmp", ".webp"],
            "文档": [".doc", ".docx", ".pdf", ".txt", ".xls", ".xlsx", ".ppt", ".pptx"],
            "视频": [".mp4", ".mov", ".avi", ".flv", ".mkv"],
            "音频": [".mp3", ".wav", ".flac", ".aac"],
            "压缩包": [".zip", ".rar", ".7z", ".tar", ".gz"]
        }
        for cat, exts in file_map.items():
            if suffix.lower() in exts:
                return cat
        return "其他"

    def batch_rename(self, prefix: str = "file"):
        # 批量重命名自动化
        files = [f for f in os.listdir(self.target_path) if os.path.isfile(os.path.join(self.target_path, f))]
        for idx, file in enumerate(files, 1):
            old_path = os.path.join(self.target_path, file)
            ext = os.path.splitext(file)[1]
            new_name = f"{prefix}_{datetime.now().strftime('%Y%m%d')}_{idx}{ext}"
            new_path = os.path.join(self.target_path, new_name)
            os.rename(old_path, new_path)

    def auto_classify(self):
        # 核心:文件自动分类移动
        for file in os.listdir(self.target_path):
            file_path = os.path.join(self.target_path, file)
            if not os.path.isfile(file_path):
                continue
            
            ext = os.path.splitext(file)[1]
            category = self.get_file_type(ext)
            target_dir = os.path.join(self.target_path, category)
            
            try:
                shutil.move(file_path, os.path.join(target_dir, file))
            except Exception:
                continue

    def agent_auto_run(self):
        # Agent 自动化一体化执行入口
        self.batch_rename(prefix="auto")
        self.auto_classify()
        return self.get_task_log()

    def get_task_log(self) -> Dict:
        # 自动化任务日志输出(供后续流程调用)
        return {
            "task_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            "task_type": "文件批量自动化处理",
            "status": "completed",
            "path": self.target_path
        }

# ==================== 本地自动化执行 ====================
if __name__ == "__main__":
    # 替换为你的本地文件夹路径
    automation = LocalFileAutomation(target_path="./downloads")
    
    # 执行一体化自动化任务
    result = automation.agent_auto_run()
    print(result)

核心技术优势

  • 纯本地标准库实现,无需安装任何依赖,开箱即用
  • 内存占用极低,老旧低配电脑可稳定后台运行
  • 支持批量重命名、自动分类、格式识别一体化任务
  • 提供标准化 Agent 调用接口,可嵌入自动化任务链
  • 可对接浏览器自动化下载目录,实现自动整理
  • 全程本地运行,不上传任何数据,安全合规
  • 代码结构可扩展,支持自定义文件规则、任务流程
posted @ 2026-03-18 12:28  xiaoyuyu666  阅读(4)  评论(0)    收藏  举报