You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python剧集重命名脚本遇FileExistsError问题求助

解决剧集重命名脚本的FileExistsError问题

问题根源

你的脚本触发FileExistsError的核心原因是对已规范命名的文件集数提取错误:

  • 当文件已是[剧名] S[季数]EP[集数]格式时,原脚本仅用r'Episode (\d+)'提取集数,无法匹配到内容,会默认使用"01"作为集数
  • 后续的规范检查逻辑用默认的01去匹配文件名(比如检查剧名 S2EP01是否在剧名 S2EP02.mp4中),显然不成立,脚本就会尝试将该文件重命名为EP01,但目标文件已存在,最终报错

修复后的代码

import os
import re

def rename_tv_episodes(root):
    for show_name in os.listdir(root):
        show_path = os.path.join(root, show_name)
        
        if os.path.isdir(show_path):
            for season_folder in os.listdir(show_path):
                season_path = os.path.join(show_path, season_folder)

                if os.path.isdir(season_path):
                    # 提取季数逻辑不变
                    match_season = re.search(r'Season (\d+)', season_folder)
                    season_number = match_season.group(1) if match_season else "1"
                    
                    for filename in os.listdir(season_path):
                        file_path = os.path.join(season_path, filename)

                        # 检查是否为视频文件
                        if os.path.isfile(file_path) and filename.endswith(('.mp4', '.mkv', '.avi', '.mov', '.wmv')):
                            # 优化集数提取:优先从规范格式提取,再从Episode格式提取
                            match_episode规范 = re.search(r'EP(\d+)', filename)
                            match_episode原始 = re.search(r'Episode (\d+)', filename)
                            
                            if match_episode规范:
                                episode_number = match_episode规范.group(1)
                            elif match_episode原始:
                                episode_number = match_episode原始.group(1)
                            else:
                                episode_number = "01"
                            
                            # 正则匹配完整的目标命名格式,确保准确判断是否已规范
                            target_pattern = re.compile(re.escape(f'{show_name} S{season_number}EP{episode_number.zfill(2)}') + r'\.[a-zA-Z0-9]+$')
                            if target_pattern.match(filename):
                                print(f'Skipped: Already follows the preferred naming scheme - {file_path}')
                                continue

                            # 生成新文件名
                            new_filename = f'{show_name} S{season_number}EP{episode_number.zfill(2)}{os.path.splitext(filename)[1]}'
                            new_file_path = os.path.join(season_path, new_filename)

                            # 重命名前先判断目标文件是否存在(额外保险)
                            if os.path.exists(new_file_path):
                                print(f'Skipped: Target file already exists - {new_file_path}')
                                continue
                            
                            os.rename(file_path, new_file_path)
                            print(f'Renamed: {file_path} -> {new_file_path}')

# 示例使用
root_directory = r'C:\Users\Jordon\Desktop\Test Folder'
rename_tv_episodes(root_directory)

关键修改点

  1. 多源集数提取:新增从规范格式EPxx中提取集数的逻辑,优先识别已规范的文件名,避免默认值错误
  2. 精准规范检查:用正则匹配完整的目标文件名格式(包含后缀),确保不会误判已规范的文件
  3. 额外存在性检查:重命名前主动判断目标文件是否存在,即使出现意外情况也能避免报错

内容的提问来源于stack exchange,提问作者MindCrasher11

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 19:44:54