python 文件目录深搜批量重命名

python真香,我马上放下了手里的C++ boost.h。
存个代码以后参考。

import os


def rename_dfs(cur_path):
    file_names = os.listdir(cur_path)

    for old_name in file_names:
        # print(old_name)
        if os.path.isdir(os.path.join(cur_path, old_name)):
            rename_dfs(os.path.join(cur_path, old_name))
            continue

        new_name = old_name
        name_list = new_name.split('.')
        if name_list.count('in') > 0:
            name_list = list(filter(lambda x: x != 'in', name_list))
            new_name = '_'.join(name_list) + '.in'
        elif name_list.count('out') > 0:
            name_list = list(filter(lambda x: x != 'out', name_list))
            new_name = '_'.join(name_list) + '.out'

        os.renames(os.path.join(cur_path, old_name), os.path.join(cur_path, new_name))


# path为批量文件的文件夹根目录
path = '.\\workspace'
rename_dfs(path)

猜你喜欢

转载自blog.csdn.net/Tighway/article/details/108503232