代码十几秒的事,为什么要手动去改一天呢!直接放源码~

重命名指定目录下所有文件夹

import os
import re

def extract_first_three_numbers_from_folder_name(folder_name):
    numbers = re.findall(r'\d+', folder_name)
    return numbers[:3]

def rename_folders_with_prefixes(directory):
    # 获取目标目录下的所有文件夹
    folder_names = [name for name in os.listdir(directory) if os.path.isdir(os.path.join(directory, name))]

    for folder_name in folder_names:
        # 提取文件夹名称中的前三个数字部分
        numbers = extract_first_three_numbers_from_folder_name(folder_name)

        # 只有在数字数量满足条件时才进行重命名
        if len(numbers) == 3:
            # 在每个数字前添加相应的字符
            prefixed_numbers = [f'V{numbers[0]}', f'D{numbers[1]}', f'Q{numbers[2]}']

            # 构建新的文件夹名称
            new_folder_name = "_".join(prefixed_numbers)

            # 构建完整的路径
            old_path = os.path.join(directory, folder_name)
            new_path = os.path.join(directory, new_folder_name)

            # 重命名文件夹
            os.rename(old_path, new_path)
            print(f"已将文件夹 {folder_name} 重命名为 {new_folder_name}")
        else:
            print(f"文件夹 {folder_name} 的数字数量不足三个,未进行重命名")

# 替换为你的目标目录路径
target_directory = r'G:\jet_data\Jet_Forward_Underwater\V3456_D3_Q7'

rename_folders_with_prefixes(target_directory)

重命名指定目录下所有文件

import os
import re

def extract_numbers_from_file_names(directory):
    # 获取目录下的所有文件
    file_names = [name for name in os.listdir(directory) if os.path.isfile(os.path.join(directory, name))]

    # 使用正则表达式提取文件名中的数字
    numbers_list = []
    pattern = re.compile(r'\d+')

    for file_name in file_names:
        matches = pattern.findall(file_name)
        numbers_list.append(matches)

    return numbers_list

def rename_files_with_prefix_suffix(directory):
    # 获取目录下的所有文件
    file_names = [name for name in os.listdir(directory) if os.path.isfile(os.path.join(directory, name))]

    # 使用正则表达式提取文件名中的数字
    numbers_list = extract_numbers_from_file_names(directory)

    # 遍历文件并进行重命名
    for old_name, numbers in zip(file_names, numbers_list):
        # 判断提取的数字个数是否小于 6
        if len(numbers) < 6:
            continue

        # 获取文件名和后缀
        file_name, file_extension = os.path.splitext(old_name)

        # 获取数字列表的最后一个数字
        last_number = numbers[-1]

        # 截取最后一个数字的后六位数字
        last_six_digits = last_number[-6:]

        # 构造新文件名
        new_name = f'V{numbers[0]}_D{numbers[1]}_Q{numbers[2]}_{last_six_digits}{file_extension}'

        # 构造新文件的完整路径
        old_path = os.path.join(directory, old_name)
        new_path = os.path.join(directory, new_name)

        # 重命名文件
        os.rename(old_path, new_path)

# 替换为你的目标目录路径
target_directory = r'G:\jet_data\test\V3_D5_Q4'

# 调用重命名函数
rename_files_with_prefix_suffix(target_directory)
print("文件重命名完成。")

重命名指定目录下所有文件夹内的所有文件

import os
import re

def extract_numbers_from_file_names(directory):
    # 获取目录下的所有文件
    file_names = [name for name in os.listdir(directory) if os.path.isfile(os.path.join(directory, name))]

    # 使用正则表达式提取文件名中的数字
    numbers_list = []
    pattern = re.compile(r'\d+')

    for file_name in file_names:
        matches = pattern.findall(file_name)
        numbers_list.append(matches)

    return numbers_list

def rename_files_with_prefix_suffix_in_subdirectories(root_directory):
    # 获取根目录下的所有文件夹
    subdirectories = [d for d in os.listdir(root_directory) if os.path.isdir(os.path.join(root_directory, d))]

    # 遍历每个文件夹
    for subdirectory in subdirectories:
        subdirectory_path = os.path.join(root_directory, subdirectory)
        
        # 获取目录下的所有文件
        file_names = [name for name in os.listdir(subdirectory_path) if os.path.isfile(os.path.join(subdirectory_path, name))]
        
        # 使用正则表达式提取文件名中的数字
        numbers_list = extract_numbers_from_file_names(subdirectory_path)

        # 遍历文件并进行重命名
        for old_name, numbers in zip(file_names, numbers_list):
            # 判断提取的数字个数是否小于 6
            if len(numbers) < 6:
                continue

            # 获取文件名和后缀
            file_name, file_extension = os.path.splitext(old_name)

            # 获取数字列表的最后一个数字
            last_number = numbers[-1]

            # 截取最后一个数字的后六位数字
            last_six_digits = last_number[-6:]

            # 构造新文件名
            new_name = f'V{numbers[0]}_D{numbers[1]}_Q{numbers[2]}_{last_six_digits}{file_extension}'

            # 构造新文件的完整路径
            old_path = os.path.join(subdirectory_path, old_name)
            new_path = os.path.join(subdirectory_path, new_name)

            # 重命名文件
            os.rename(old_path, new_path)

# 替换为你的目标根目录路径
root_directory = r'G:\jet_data\Jet_Forward_Collapse'

# 调用重命名函数
rename_files_with_prefix_suffix_in_subdirectories(root_directory)
print("文件重命名完成。")