path1是一组图片,path2是标记图片对应的XML文件,
path2和path1本应该所有文件的文件名都相同(不包括后缀),但是path2不小心混入了一些其他文件,
现在要删掉path2下多出的文件:
1 # coding=UTF-8 2 import os 3 4 def find(path): 5 data = [] 6 for root, dirs, files in os.walk(path): 7 for file in files: 8 data.append(os.path.splitext(file)[0]) 9 return data 10 11 def redundant(path, data): 12 extra = [] 13 for root, dirs, files in os.walk(path): 14 for file in files: 15 name = os.path.splitext(file)[0] 16 if name not in data: 17 extra.append(name) 18 return extra 19 20 def removal(path, data): 21 for root, dirs, files in os.walk(path): 22 for file in files: 23 name = os.path.splitext(file)[0] 24 if name not in data: 25 os.remove(root + '/' + file) 26 27 path1 = 'C:/Users/Administrator/Desktop/7.20/1' 28 path2 = 'C:/Users/Administrator/Desktop/7.20/xml1' 29 30 #此目录下所有文件名(不包括后缀)放入data列表 31 data = find('path1') 32 #此目录相比上一个目录多出的文件名 33 extra = redundant('path2', data) 34 35 print(len(extra)) 36 #删掉多出的文件 37 removal('path2', data)