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

如何基于另一文件夹文件名用Python批量重命名XML文件?

Solution for Batch Renaming XML Files Based on Corresponding Image Files

Hey Maria, let's solve this batch renaming problem efficiently—since you've got thousands of files, we'll use a Python script to automate the process reliably. Here's how to do it:

Step 1: Understand the Mapping Logic

First, we need to build a prefix-to-number map from Folder 2's image files. For example:

  • 05_2504.jpeg → map 05 to 2504
  • 06_2505.jpeg → map 06 to 2505

Then, we'll use this map to rename Folder 1's XML files:

  • 05.xml → 2504.xml
  • 06.xml → 2505.xml

Step 2: Python Script Implementation

Here's a complete script that handles this. Just replace folder1_path and folder2_path with your actual folder paths:

import os

# Replace these with your actual folder paths
folder1_path = "/path/to/your/folder1"  # Contains 05.xml, 06.xml, etc.
folder2_path = "/path/to/your/folder2"  # Contains 05_2504.jpeg, etc.

# Step 1: Build the prefix-to-number map from Folder 2
prefix_map = {}
for filename in os.listdir(folder2_path):
    # Skip non-jpeg files (adjust extension if needed)
    if not filename.endswith(".jpeg"):
        continue
    # Split filename into prefix and number part (split on underscore)
    parts = filename.split("_")
    if len(parts) != 2:
        print(f"Skipping invalid filename: {filename}")
        continue
    prefix = parts[0]
    # Extract the number (remove .jpeg extension)
    number_part = parts[1].replace(".jpeg", "")
    prefix_map[prefix] = number_part

# Step 2: Rename XML files in Folder 1 using the map
for filename in os.listdir(folder1_path):
    if not filename.endswith(".xml"):
        continue
    # Get the prefix from the XML filename (remove .xml extension)
    xml_prefix = filename.replace(".xml", "")
    # Check if we have a matching number in the map
    if xml_prefix in prefix_map:
        new_filename = f"{prefix_map[xml_prefix]}.xml"
        old_file_path = os.path.join(folder1_path, filename)
        new_file_path = os.path.join(folder1_path, new_filename)
        # Rename the file
        os.rename(old_file_path, new_file_path)
        print(f"Renamed: {filename} → {new_filename}")
    else:
        print(f"No matching image found for: {filename}")

Step 3: Why Your Split Attempt Might Have Failed

If your initial split approach didn't work, here are common pitfalls to check:

  • You might have split on the wrong character (e.g., using . instead of _ for the image filenames)
  • You didn't properly handle file extensions when extracting the prefix/number
  • You didn't build a persistent map to link the XML prefixes to the corresponding numbers from the images

Notes for Large Scale (Thousands of Files)

  • This script is efficient enough for thousands of files—Python's os module handles file operations quickly.
  • Always back up your files first before running batch renaming scripts, just in case!
  • If you have other image extensions (like .jpg), adjust the endswith check in the script.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:39:15