批量实现文件夹下所有文件的文件名英译中

因为工作原因,涉及很多英文文件,全部都是英文名,不利于检索,每次都很痛苦,做了个python,批量进行翻译,以后都是中文了,方便快速检索使用,有需要的朋友可以试试,如果有更好的库,欢迎交流

# 导入os模块和translate模块
import os
from translate import Translator

# 定义一个函数,接受一个文件夹路径和一个语言方向作为参数
def translate_and_rename(folder_path, lang_dir):
    # 遍历文件夹下的所有文件
    for file in os.listdir(folder_path):
        # 获取文件的完整路径
        file_path = os.path.join(folder_path, file)
        # 判断是否是文件,如果是,则进行翻译和重命名
        if os.path.isfile(file_path):
            # 根据语言方向,确定源语言和目标语言的代码
            if lang_dir == "zh2en":
                source_lang = "zh"
                target_lang = "en"
            elif lang_dir == "en2zh":
                source_lang = "en"
                target_lang = "zh"
            else:
                print(f"无效的语言方向:{lang_dir},请使用'zh2en'或'en2zh'")
                return
            # 创建一个translate模块的Translator对象,指定源语言和目标语言
            translator = Translator(from_lang=source_lang, to_lang=target_lang)
            # 调用Translator对象的translate方法,将文件名翻译成目标语言
            translated_name = translator.translate(file)
            # 如果翻译成功,则生成新的文件路径
            if translated_name:
                new_file_path = os.path.join(folder_path, translated_name)
                # 尝试重命名文件,如果失败,则打印错误信息
                try:
                    os.rename(file_path, new_file_path)
                    print(f"成功将{file}重命名为{translated_name}")
                except OSError as e:
                    print(f"无法重命名{file},错误原因:{e}")
            else:
                print(f"无法翻译{file},请检查网络连接或语言代码")


# 调用函数,传入要处理的文件夹路径和语言方向
translate_and_rename("D:\\Personal\\桌面\\1", "en2zh")

在这个脚本基础上优化 @echo off setlocal enabledelayedexpansion :: ---------------------------- :: 基础配置(可修改区域) :: ---------------------------- set “UV=F:\Software\keil5\core\UV4\UV4.exe” set “project_root=%~dp0Multi_project” :: 工程根目录 :: ---------------------------- :: 命令行参数处理 :: ---------------------------- if not “%~1”==“” ( set “TARGET_PROJECT=%~1” call :validate_project call :compile exit /b %errorlevel% ) :: ---------------------------- :: 自动检测所有子工程 :: ---------------------------- echo Scanning projects in: %project_root% set “project_count=0” for /d %%a in (“%project_root%*”) do ( set /a “project_count+=1” set “project_name=%%~nxa” set “project_list=!project_list! !project_name!” ) echo Found %project_count% project(s) :: ---------------------------- :: 主编译流程 :: ---------------------------- for %%a in (%project_list%) do ( set “TARGET_PROJECT=%%a” call :compile if errorlevel 1 ( echo [ERROR] Build failed for: %%a exit /b 1 ) ) exit /b 0 :: ---------------------------- :: 子程序:工程验证 :: ---------------------------- :validate_project if not exist “%project_root%%TARGET_PROJECT%” ( echo [ERROR] Project not exist: %TARGET_PROJECT% exit /b 2 ) if not exist “%project_root%%TARGET_PROJECT%%TARGET_PROJECT%_MCU.uvmpw” ( echo [ERROR] Project file missing: %TARGET_PROJECT%_MCU.uvmpw exit /b 3 ) exit /b 0 :: ---------------------------- :: 子程序:编译流程 :: ---------------------------- :compile echo. echo [INFO] Building project: %TARGET_PROJECT% :: 路径配置(自动生成带时间戳的日志文件) set “build_log=%~dp0logs%TARGET_PROJECT%%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%.log” set “uvproj_path=%project_root%%TARGET_PROJECT%%TARGET_PROJECT%_MCU.uvmpw” :: 清理并创建日志目录 if not exist “%~dp0logs” md “%~dp0logs” break > “%build_log%” :: 执行编译命令 echo Project file: %uvproj_path% “%UV%” -j0 -r “%uvproj_path%” -o “%build_log%” :: ---------------------------- :: 错误检查 :: ---------------------------- if not exist “%build_log%” ( echo [ERROR] Log file missing: %build_log% exit /b 4 ) type “%build_log%” | findstr /i /r “<Error>” “<error(s)>” “<错误>” if %errorlevel% equ 0 ( echo [ERROR] Compilation failed: %TARGET_PROJECT% exit /b 1 ) echo [SUCCESS] Build completed: %TARGET_PROJECT% exit /b 0
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wh3933

你的

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值