在各个子文件夹中有多个图片,也有多个非图片文件,如:000001
使用以下代码即可删除非图片数据了。
# -*- coding: utf-8 -*-
import os
import cv2
path = "./datasets" # 根目录
file = os.listdir(path)
for file1 in file:
path1 = path + "/" + file1
file1 = os.listdir(path1)
for filename in file1:
temp = path1 + "/" + filename
f = os.path.splitext(filename)
#print(f[1])
if f[1] != '.tif' and f[1]!='.jpg':
print(temp)
os.remove(temp)
注:以上代码实现的是二级文件夹,如果数据有多级文件夹,可自行加循环。
tif和jpg 如果需要其他后缀图片,可以自行添加。