python列表去重:
import os fileName=os.listdir(r'地址') list1=[] for file in fileName: if file not in list1: list1.append(file) print(list1)
python递归提取文件:
import os,shutil FileList=[] def getfile(path):#可以先定义一个path地址 allfile=os.listdir(path) print(allfile) for file in allfile: filepath=os.path.join(path,file) if os.path.isdir(filepath): print("目录:",file) getfile(filepath) elif os.path.isfile(filepath): print("底层文件:",file) FileList.append(file) shutil.copy(filepath,r'C:\Users\Administrator\Desktop\001')#保存到另一个文件夹
CV2图像resize:
import os import cv2 path='C:/Users/Administrator/Desktop/001/' ImageNames=os.listdir(path) # 单体处理 # image=cv2.imread('C:/Users/Administrator/Desktop/001/S0027_z100292_20190419092026.jpg') # response=cv2.resize(image,(600,480),interpolation=cv2.INTER_CUBIC) # cv2.imwrite('C:/Users/Administrator/Desktop/001/S0027_z100292_20190419092026.jpg',response) #批量处理测试 for file in ImageNames: image=cv2.imread(os.path.join(path,file))#批量处理不同于单体处理的地方就是这里一定递归把文件路径组合起来 rsp=cv2.resize(image,(600,480),interpolation=cv2.INTER_CUBIC) cv2.imwrite('C:/Users/Administrator/Desktop/001/'+file,rsp)
python 提取前缀名相同的文件:
import os import shutil path=r'C:\Users\Administrator\Desktop\001' fileName=os.listdir(path) for file in fileName: filepath=os.path.join(path,file) if file.startswith('void'): shutil.copy(filepath,r'C:\Users\Administrator\Desktop\002')
python过滤文件不对应:
import os def select_xml(): list = os.listdir(r'C:\Users\Administrator\Desktop\002') list1=[] for item in list: if item.endswith(".xml"): list1.append(item.rsplit('.xml')) return list1 def select_jpg(): list = os.listdir(r'C:\Users\Administrator\Desktop\002') list2 = [] for item in list: if item.endswith(".jpg"): list2.append(item.rsplit('.jpg')) return list2 a=select_xml() b=select_jpg() def panduan_():#这个函数的功能是判断.jpg后缀的图片有没有对应的.xml文件,如果没有就打印出来 list3=[] for i in range(0,len(a)-1): if a[i] not in b: print(a[i]) list3.append(a[i]) return list3 panduan_() if __name__ == '__main__': panduan_()