删掉加载出错的文件:
import PIL.Image as Image
import os
import warnings
def check_img_valid(img_path):
result = False
try:
Image.open(img_path)
result = True
except UserWarning as msg:
print(msg)
print(img_path)
return result
warnings.filterwarnings("error", category=UserWarning)
path = r'C:\xxxxx\data'
files = os.listdir(path)
for file in files:
new_path = os.path.join(path, file)
if not check_img_valid(new_path):
os.remove(new_path)