pip install Pillow
# 功能:查看图片能否打开,有没有这个图片、是不是完好、没有损坏的图片 # 参数:图片路径 # 返回:True from PIL import Image def check_pic(path_pic): try: # FileNotFoundError img = Image.open(path_pic) # OSError: image file is truncated img.load() except (FileNotFoundError, OSError): return False else: return True
内置xml库
# 确认XML文件是否完整 from xml.sax.handler import ContentHandler from xml.sax import make_parser def parseFile(filepath): try: parser = make_parser() parser.setContentHandler(ContentHandler()) parser.parse(filepath) except Exception: return False else: return True