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

使用os.rename()重命名火影视频时触发WinError 123错误的解决办法

Fixing WinError 123 When Renaming Naruto Shippuden Files in Python

Got it, let's break down why you're hitting this WinError 123 and fix it step by step.

The Core Issue

First off, that error is happening because Windows doesn't allow colons (:) in file names. Your new file name format includes Episode: 003, which violates Windows' filename syntax rules—that's exactly why you're getting the "filename syntax incorrect" error.

Step-by-Step Fixes

1. Replace the Colon in Your New Filename

Change the colon to something Windows accepts—like a dash, or just remove it entirely. For example, update your new_name line to:

new_name = f"{mo.group(1).title()} {mo.group(3).title()}, Episode - {mo.group(7)}{ext[1]}"

Or if you prefer a cleaner look without extra punctuation:

new_name = f"{mo.group(1).title()} {mo.group(3).title()} Episode {mo.group(7)}{ext[1]}"

2. Add Safeguards to Avoid Other Potential Issues

While we're at it, let's make the script more robust to prevent other headaches:

  • Skip folders: Don't try to rename directories in your target folder.
  • Filter non-video files: Only process actual video files (like .mkv, .mp4, .avi) to avoid accidental changes to other files.
  • Use absolute paths: Avoid relying on os.chdir()—it can lead to unexpected behavior if your script runs in the wrong context.

Updated Full Script

Here's the revised script with all these fixes:

import os
import re

def rename_files():
    folder_path = r"C:\Users\Caleb\Videos\Anime\Naruto Shippuden"
    # Define valid video extensions to process
    valid_extensions = ('.mkv', '.mp4', '.avi', '.mov')
    
    for file in os.listdir(folder_path):
        file_path = os.path.join(folder_path, file)
        
        # Skip directories
        if not os.path.isfile(file_path):
            print(f"Skipping directory: {file}")
            continue
        
        # Skip non-video files
        ext = os.path.splitext(file)
        if ext[1].lower() not in valid_extensions:
            print(f"Skipping non-video file: {file}")
            continue
        
        # Your regex pattern (left as-is, but you can verify groups if needed)
        nameRegex = re.compile(r'.?(Naruto(-?)).'
                              r'(Shi(p)+(u)+den)'
                              r'(_-_|\\s-\\s|\\sEpisode\\s|-_Season_17_Episode_| Episode |_)'
                              r'((\\d\\d\\d)(-\\d\\d\\d)?)', re.I)
        mo = nameRegex.search(file)
        
        if mo is None:
            print(f'No match found for: {file}')
            continue
        
        # Generate new name without illegal characters
        new_name = f"{mo.group(1).title()} {mo.group(3).title()}, Episode - {mo.group(7)}{ext[1]}"
        new_file_path = os.path.join(folder_path, new_name)
        
        # Optional: Print before renaming to verify changes
        print(f"Renaming: {file} -> {new_name}")
        
        # Perform the rename
        os.rename(file_path, new_file_path)

rename_files()

Quick Debug Tip

If you're unsure about your regex groups, add print(mo.groups()) right after the mo = nameRegex.search(file) line. This will show you all captured groups, so you can confirm that mo.group(7) is indeed the episode number you want.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:17:04