一、读取txt文档
txt_file = r'F:\txt_xml_nohead\tsak0.txt' file = open(txt_file,"r",encoding='utf-8') lines = file.readlines() for line in lines: print(line)
二、遍历文件夹下的图片
def getFileList(dir, Filelist, ext=None): """ 获取文件夹及其子文件夹中文件列表 输入 dir:文件夹根目录 输入 ext: 扩展名 返回: 文件路径列表 """ newDir = dir if os.path.isfile(dir): if ext is None: Filelist.append(dir) else: if ext in dir[-3:]: Filelist.append(dir) elif os.path.isdir(dir): for s in os.listdir(dir): newDir = os.path.join(dir, s) getFileList(newDir, Filelist, ext) return Filelist
三、从文件夹下删除图片
if __name__ == '__main__': txt_file = r'E:\ReID\head图片标注\标注1-34\task24-29,34\没head编码.txt' org_img_folder = r'E:\ReID\head图片标注\head未标注\personTask\task24\task24\pic' #org_img_folder = r'E:\ReID\head图片标注\标注1-34\task24-29,34\xml' imglist = getFileList(org_img_folder, [], 'jpg') print('本次执行检索到 ' + str(len(imglist)) + ' 张图像\n') # 读取txt file = open(txt_file, "r", encoding='utf-8') lines = file.readlines() i = 0 for line in lines: i += 1 #line = os.path.splitext(line)[0] line = line.strip('\n') # 去掉换行符 # if line == " ": # print('line:', line) # file.write(line.replace(" ", "")) for imgpath in imglist: imgname = os.path.splitext(os.path.basename(imgpath))[0] if line == imgname: #print('imgname:',imgname) os.remove(imgpath) print('需移除的文件有:',i)
四、代码解析
3.1 os.path.splitext()
将文件名和扩展名分开。参考
3.2 os.path.basename()
返回path最后的文件名,若path以/或\结尾,那么就会返回空值。参考
path='D:\CSDN'
os.path.basename(path)=CSDN