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

Python递增文件夹创建、文件移动及特定规则重命名问题求助

文件批量移动与重命名解决方案

核心需求

检测父文件夹中的文件,每次将12个文件移至命名递增的新文件夹(遵循Windows资源管理器重复文件夹的命名逻辑,如批量文件夹、批量文件夹(1)、批量文件夹(2)...),同时将T1R1C1L3格式的文件名重命名为T1R1C1L03(L后替换为两位递增数字)。


1. 循环创建递增命名的文件夹

实现Windows风格的递增命名逻辑:先检查基础名称文件夹是否存在,若存在则遍历所有带括号后缀的文件夹,找到最大编号后生成下一个名称;若不存在则直接使用基础名称。

import os
import re

def get_next_folder_name(base_path, base_name="批量文件夹"):
    # 检查基础文件夹是否存在
    if not os.path.exists(os.path.join(base_path, base_name)):
        return base_name
    # 匹配带括号的文件夹名,如"批量文件夹(1)"
    pattern = re.compile(rf"{re.escape(base_name)}\((\d+)\)")
    max_num = 0
    for item in os.listdir(base_path):
        if os.path.isdir(os.path.join(base_path, item)):
            match = pattern.match(item)
            if match:
                num = int(match.group(1))
                if num > max_num:
                    max_num = num
    return f"{base_name}({max_num + 1})"

# 使用示例
parent_dir = "./父文件夹路径"
next_folder = get_next_folder_name(parent_dir)
new_folder_path = os.path.join(parent_dir, next_folder)
os.makedirs(new_folder_path, exist_ok=True)

2. 将文件移至刚创建的文件夹

创建文件夹后,直接用新文件夹路径作为目标路径,配合shutil.move完成移动(可结合重命名操作):

import shutil

# 假设已获取待移动的12个文件列表file_list
for idx, file in enumerate(file_list, start=1):
    old_path = os.path.join(parent_dir, file)
    # 这里可插入重命名逻辑生成新文件名
    new_path = os.path.join(new_folder_path, 新文件名)
    shutil.move(old_path, new_path)

3. 重命名文件为两位递增数字格式

针对T1R1C1L3格式的文件名,提取L前的前缀,为每个文件分配两位递增数字(如L01、L02...L12):

def rename_file_with_two_digit_l(file_name, index):
    # 拆分文件名和扩展名
    file_name_no_ext, ext = os.path.splitext(file_name)
    # 替换L后的数字为两位递增格式
    new_file_name = re.sub(r"L\d+", f"L{index:02d}", file_name_no_ext)
    return f"{new_file_name}{ext}"

# 使用示例
new_file_name = rename_file_with_two_digit_l("T1R1C1L3.txt", 3)
# 输出:T1R1C1L03.txt

完整整合代码

将三个模块整合,实现完整的批量移动+重命名逻辑:

import os
import re
import shutil

def get_next_folder_name(base_path, base_name="批量文件夹"):
    if not os.path.exists(os.path.join(base_path, base_name)):
        return base_name
    pattern = re.compile(rf"{re.escape(base_name)}\((\d+)\)")
    max_num = 0
    for item in os.listdir(base_path):
        if os.path.isdir(os.path.join(base_path, item)):
            match = pattern.match(item)
            if match:
                num = int(match.group(1))
                if num > max_num:
                    max_num = num
    return f"{base_name}({max_num + 1})"

def rename_file_with_two_digit_l(file_name, index):
    name, ext = os.path.splitext(file_name)
    new_name = re.sub(r"L\d+", f"L{index:02d}", name)
    return f"{new_name}{ext}"

def batch_move_and_rename(parent_dir, batch_size=12):
    # 获取父文件夹下的所有文件(排除子文件夹)
    all_files = [f for f in os.listdir(parent_dir) if os.path.isfile(os.path.join(parent_dir, f))]
    # 按批次处理文件
    for i in range(0, len(all_files), batch_size):
        batch_files = all_files[i:i+batch_size]
        if not batch_files:
            continue
        # 创建递增命名的文件夹
        next_folder = get_next_folder_name(parent_dir)
        new_folder_path = os.path.join(parent_dir, next_folder)
        os.makedirs(new_folder_path, exist_ok=True)
        # 移动并重命名当前批次的文件
        for idx, file in enumerate(batch_files, start=1):
            old_path = os.path.join(parent_dir, file)
            new_file_name = rename_file_with_two_digit_l(file, idx)
            new_path = os.path.join(new_folder_path, new_file_name)
            shutil.move(old_path, new_path)

# 执行主函数(替换为实际父文件夹路径)
if __name__ == "__main__":
    target_parent_dir = "./你的父文件夹路径"
    batch_move_and_rename(target_parent_dir)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 03:23:11