Python script won't skip past files that match naming convention set in file renaming program

34 views Asked by At

I have a python program that renames tv show files to match a preferred naming convention I prefer but it only works on episodes that don't match the convention. When it gets to the first of matching episodes it skips it but once it runs into another it it gets a file exist error. I've tried getting past it using episode increments but that just changes the episode number so it no longer matches the original, so I reverted to what works best so far.

Here is the code that I've gotten working the best:

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):
                    for filename in os.listdir(season_path):
                        file_path = os.path.join(season_path, filename)

                        # Check if the file is a video (you can add more extensions as needed)
                        if os.path.isfile(file_path) and filename.endswith(('.mp4', '.mkv', '.avi', '.mov', '.wmv')):
                            # Extract season number
                            match_season = re.search(r'Season (\d+)', season_folder)
                            season_number = match_season.group(1) if match_season else "1"

                            # Extract episode number
                            match_episode = re.search(r'Episode (\d+)', filename)
                            episode_number = match_episode.group(1) if match_episode else "01"

                            # Check if the file is already following the preferred naming scheme
                            if f'{show_name} S{season_number}EP{episode_number.zfill(2)}' in filename:
                                print(f'Skipped: Already follows the preferred naming scheme - {file_path}')
                                continue

                            # Create new filename
                            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)

                            # Rename the file
                            os.rename(file_path, new_file_path)
                            print(f'Renamed: {file_path} -> {new_file_path}')

# Example usage
root_directory = r'C:\Users\Jordon\Desktop\Test Folder'
rename_tv_episodes(root_directory)

Here is the output:

Renamed: C:\Users\Jordon\Desktop\Test Folder\In Another World With My Smartphone\Season 1\In Another World With My Smartphone Episode 1.mp4 -> C:\Users\Jordon\Desktop\Test Folder\In Another World With My Smartphone\Season 1\In Another World With My Smartphone S1EP01.mp4
Renamed: C:\Users\Jordon\Desktop\Test Folder\In Another World With My Smartphone\Season 1\In Another World With My Smartphone Episode 10.mp4 -> C:\Users\Jordon\Desktop\Test Folder\In Another World With My Smartphone\Season 1\In Another World With My Smartphone S1EP10.mp4
Renamed: C:\Users\Jordon\Desktop\Test Folder\In Another World With My Smartphone\Season 1\In Another World With My Smartphone Episode 11.mp4 -> C:\Users\Jordon\Desktop\Test Folder\In Another World With My Smartphone\Season 1\In Another World With My Smartphone S1EP11.mp4
Renamed: C:\Users\Jordon\Desktop\Test Folder\In Another World With My Smartphone\Season 1\In Another World With My Smartphone Episode 12.mp4 -> C:\Users\Jordon\Desktop\Test Folder\In Another World With My Smartphone\Season 1\In Another World With My Smartphone S1EP12.mp4
Renamed: C:\Users\Jordon\Desktop\Test Folder\In Another World With My Smartphone\Season 1\In Another World With My Smartphone Episode 2.mp4 -> C:\Users\Jordon\Desktop\Test Folder\In Another World With My Smartphone\Season 1\In Another World With My Smartphone S1EP02.mp4
------------SKIP(not output)-----------
Skipped: Already follows the preferred naming scheme - C:\Users\Jordon\Desktop\Test Folder\In Another World With My Smartphone\Season 2\In Another World With My Smartphone S2EP01.mp4
Traceback (most recent call last):
  File "C:\Users\Jordon\Desktop\TV_Show_Episode_Renamer_v3.py", line 41, in <module>
    rename_tv_episodes(root_directory)
  File "C:\Users\Jordon\Desktop\TV_Show_Episode_Renamer_v3.py", line 36, in rename_tv_episodes
    os.rename(file_path, new_file_path)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\\Users\\Jordon\\Desktop\\Test Folder\\In Another World With My Smartphone\\Season 2\\In Another World With My Smartphone S2EP02.mp4' -> 'C:\\Users\\Jordon\\Desktop\\Test Folder\\In Another World With My Smartphone\\Season 2\\In Another World With My Smartphone S2EP01.mp4'
0

There are 0 answers