python 批量重命名


import os

def remove_filename(path_dir, start, end):
    files = [f for f in os.listdir(path_dir) if f.startswith(start) and f.endswith(end)]
    for file_name in files:
        new_file_name= file_name.replace(start,'')
        os.chdir(path_dir)
        try:
            if not os.path.exists(new_file_name):
                print(file_name, ' changed to ', new_file_name)
                os.rename(file_name, new_file_name)
        except Exception as e:
            print(e)

posted @ 2021-01-24 11:28  该显示昵称已被使用了  阅读(85)  评论(0编辑  收藏  举报