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)
本文来自博客园,作者:海_纳百川,转载请注明原文链接:https://www.cnblogs.com/chentiao/p/17479212.html,如有侵权联系删除

浙公网安备 33010602011771号