file_name=[]
file_name = os.listdir("D:\\Work\\process\\test\\polyu-all-train\\")
with open("output-train.txt", "w+") as fp:
for i in file_name:
for j in range(26):
name = "_p" + str(j+1) + "_"
if i.find(name) != -1:
namelabel = j+1
newname ="polyu\\"+ "p"+str(namelabel) +"\\"+i + " " + str(namelabel) +'\n'
fp.write(newname)
结果:
polyu\p10\s10_p10_c1.jpg 10
polyu\p10\s10_p10_c2.jpg 10
polyu\p10\s10_p10_c3.jpg 10
polyu\p10\s10_p10_c4.jpg 10
polyu\p11\s10_p11_c1.jpg 11
polyu\p11\s10_p11_c2.jpg 11
polyu\p11\s10_p11_c3.jpg 11
polyu\p11\s10_p11_c4.jpg 11
polyu\p12\s10_p12_c1.jpg 12
polyu\p12\s10_p12_c2.jpg 12
polyu\p12\s10_p12_c3.jpg 12
polyu\p12\s10_p12_c4.jpg 12
polyu\p13\s10_p13_c1.jpg 13
polyu\p13\s10_p13_c2.jpg 13
polyu\p13\s10_p13_c3.jpg 13
polyu\p13\s10_p13_c4.jpg 13
polyu\p14\s10_p14_c1.jpg 14
polyu\p14\s10_p14_c2.jpg 14
polyu\p14\s10_p14_c3.jpg 14
polyu\p14\s10_p14_c4.jpg 14
polyu\p15\s10_p15_c1.jpg 15