import os from xpinyin import Pinyin def files_rename(file_path, file_list, prefix='20230312_'): for i in range(len(file_list)): old_name = os.path.join(file_path, file_list[i]) file_name, file_type = file_list[i].split('.') num = str(i+1).rjust(5, '0') name = prefix + num + '.' + file_type new_name = os.path.join(file_path, name) # print(old_name) # print(new_name) os.rename(old_name, new_name) if __name__ == '__main__': p = Pinyin() basic_path = r'E:\spider_leaning\Images_Data_Dog' folders = ['中华田园犬', '博美犬', '吉娃娃', '哈士奇', '喜乐蒂', '巴哥犬', '德牧', '拉布拉多犬', '杜宾犬', '松狮犬', '柯基犬', '柴犬', '比格犬', '比熊', '法国斗牛犬', '秋田犬', '约克夏', '罗威纳犬', '腊肠犬', '萨摩耶', '西高地白梗犬', '贵宾犬', '边境牧羊犬', '金毛犬', '阿拉斯加犬', '雪纳瑞', '马尔济斯犬'] for folder in folders: folder_name = p.get_pinyin(folder).replace('-', '') files_path = os.path.join(basic_path, folder) file_list = os.listdir(files_path) prefix = folder_name + '20230312_' files_rename(files_path, file_list, prefix) # print(os.listdir(basic_path)) # file_list = os.listdir(basic_path) # prefix = '20230312_' # files_rename(basic_path, file_list, prefix)
标签:重命名,文件,name,list,prefix,文件夹,file,path,os From: https://www.cnblogs.com/moon3496694/p/17209437.html