Win10环境 Python批量修改文件夹名

1、遇到问题
在项目中,遇到需要将下载得到的,人脸样本数据用名字加以区分的问题 。初始文件夹如图,不能清楚地区分人脸样本 。
2、解决思路
(1)找一个随机生成英文名的网站,生成的英文名粘贴到++ 。使用“替换”调整英文名的格式 。
(2)将生成的英文名存储在列表中,使用os.()获取path下的子文件夹名,以列表形式存储
(3)文件夹新名称如下,将英文名列表中的元素按照下标附到后
newname = oldname + '-' + subname[i]
随后使用os.(,)重命名该目录
3、具体实施
(1)因为英文名需要存储在列表中,所以在++中先将其改为如下格式

Win10环境  Python批量修改文件夹名

文章插图
(2)代码
import os#导入path路径path = 'D:/SoftwareCache/PyCharmCache/Project_Face/trainer/FERET_80_80/FERET_80_80-人脸数据库/FERET_80_80-人脸数据库'#获取该目录下所有低一级别子目录,存入列表中dirlist = os.listdir(path)#需要添加在上述目录结尾的名字,存在一个列表里面subname = ['Lesley','Elva','Gregary','Leonard','Franklin','Joanna','Kim','Roxanne','Berton','Simona','Afra','Dunn','Jesse','Janice','Egbert','Hulda','Betsy','Ansel','Judy','Adair','Channing','Leila','Rita','Edwiin','Samuel','Dana','Samantha','Page','Fitch','Verne','Adolph','Rebecca','Lindsay','Oliver','Claude','Theresa','Woodrow','Jo','Gene','Rock','Hiram','Modesty','Eric','Ada','Sherry','Mike','Janet','Francis','Blair','Vivien','Roberta','Odelia','Scott','Susie','Jenny','Mary','Ford','Juliet','Harriet','Lewis','Riva','Brandon','Ethel','Sid','Matt','Renee','Evelyn','Levi','Mick','Eartha','Veronica','Kent','Lucien','Marshall','Dinah','Frank','Ward','Allen','Quincy','Joy','Maxine','Chasel','Abbott','Mandel','Burton','Ula','Amy','Anastasia','Tyler','Rosalind','Dora','Ann','Donna','Merlin','Donald','Maggie','Antonia','Nicholas','Kerwin','Clarence','Charlotte','Aries','Sandra','Julius','Chester','Constance','Rae','Cornelius','Gordon','Cornelia','Shirley','Adonis','Zona','Donahue','Bonnie','Mignon','Glenn','Winni','Heloise','Theobald','Karen','Elma','Florence','Leona','Malcolm','Kama','Miles','Astrid','Mandy','Grover','Cheryl','Zenobia','Beatrice','Andrea','Abraham','Lee','William','Ron','Spencer','Octavia','Blithe','Ives','Evan','Frances','Buck','Lawrence','Angela','Omar','Jessica','Deborah','Eve','Katherine','Colbert','Humphrey','Phil','Phoebe','Celeste','Valentina','Wendy','Aaron','Horace','Deirdre','Alvis','Drew','Brook','Basil','Hubery','Penny','Stephanie','Eden','Diana','Valentine','Jill','Rachel','Roderick','Baldwin','Devin','Selena','Edmund','Benjamin','Nelson','Cornell','Bowen','Martina','Xaviera','Elijah','Jacqueline','Claire','Morton','Alexander','Orville','Griffith','Irma','Les','Norma','Daphne','Louise','Broderick','Lyndon','Patricia']#打印这两个列表# print(dirlist)# print(subname)#i对应两个列表的角标i = 0;for dir in dirlist:#os.sep添加系统分隔符oldname = path + os.sep + dirnewname = oldname + '-' + subname[i]# dir的样式为FERET-001,new_dir的样式为 FERET-001-Lesleynew_dir = dir + '-' + subname[i]#os.rename()方法的参数必须为完整的文件名,即绝对路径os.rename(oldname,newname)print(dir,'----------->',new_dir)i += 1
(3)运行结果如下(实际有200条,方便起见截取前10条)
【Win10环境Python批量修改文件夹名】FERET-001 -----------> FERET-001-LesleyFERET-002 -----------> FERET-002-ElvaFERET-003 -----------> FERET-003-GregaryFERET-004 -----------> FERET-004-LeonardFERET-005 -----------> FERET-005-FranklinFERET-006 -----------> FERET-006-JoannaFERET-007 -----------> FERET-007-KimFERET-008 -----------> FERET-008-RoxanneFERET-009 -----------> FERET-009-BertonFERET-010 -----------> FERET-010-Simona