1、遇到问题
在项目中,遇到需要将下载得到的,人脸样本数据用名字加以区分的问题。初始文件夹如图,不能清楚地区分人脸样本。
?2、解决思路
(1)找一个随机生成英文名的网站,生成的英文名粘贴到Notepad++。使用“替换”调整英文名的格式。
(2)将生成的英文名存储在列表中,使用os.listdir()获取path下的子文件夹名,以列表形式存储
(3)文件夹新名称newname如下,将英文名列表中的元素按照下标附到oldname后
newname = oldname + '-' + subname[i]
?随后使用os.rename(oldname,newname)重命名该目录
3、具体实施
(1)因为英文名需要存储在列表中,所以在Notepad++中先将其改为如下格式
?(2)Python代码
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 + dir
newname = oldname + '-' + subname[i]
# dir的样式为FERET-001,new_dir的样式为 FERET-001-Lesley
new_dir = dir + '-' + subname[i]
#os.rename()方法的参数必须为完整的文件名,即绝对路径
os.rename(oldname,newname)
print(dir,'----------->',new_dir)
i += 1
(3)运行结果如下(实际有200条,方便起见截取前10条)
FERET-001 -----------> FERET-001-Lesley
FERET-002 -----------> FERET-002-Elva
FERET-003 -----------> FERET-003-Gregary
FERET-004 -----------> FERET-004-Leonard
FERET-005 -----------> FERET-005-Franklin
FERET-006 -----------> FERET-006-Joanna
FERET-007 -----------> FERET-007-Kim
FERET-008 -----------> FERET-008-Roxanne
FERET-009 -----------> FERET-009-Berton
FERET-010 -----------> FERET-010-Simona
|