python捕获非错误输出提示

2023年06月14日09:02:32

问题:我使用exifread.process_file()读取图片时,当图片存储格式为png时,终端输出提示:PNG file does not have exif data。但是不会报错,会继续处理其他图片。这种情况无法使用try捕获错误,因为这显然是该读取方法里使用了try捕获错误做出了提示,显然双层try不好捕获。这个时候我们该如何捕获这个非错误的提示呢?

output = StringIO()
with open(image_path, 'rb') as f:
  try:
    sys.stderr = output
    tags = exifread.process_file(f)
  finally:
    sys.stderr = sys.__stderr__
  output_string = output.getvalue()
  if 'PNG file does not have exif data' in output_string:
    print(image_path)

 

posted @ 2023-06-14 09:11  海_纳百川  阅读(63)  评论(0编辑  收藏  举报
本站总访问量