批量修改文件夹内所有的文件名(包括文件树的所有叶子,利用递归)

import os


def remove(path):
    file_list = os.listdir(path)
    for file in file_list:
        if os.path.isdir(os.path.join(path, file)):           
            remove(os.path.join(path, file))
        else:
            to_delete = '- TB Shop:DJSHANKS -'
            old = file
            if to_delete in old:
                new = old.replace(to_delete, '')  # 替换成空字符串
                os.rename(os.path.join(path, old), os.path.join(path, new))


remove(os.getcwd())

猜你喜欢

转载自blog.csdn.net/ik666/article/details/120088071