[英]How to use a if function as try except
我有一個程序,里面有一個循環。 在這個循環中,我從文件加載圖像。 但是有一些不可用的圖像,我不想手動排序。 我有這個代碼:
img = Image.open("downloads/parrot/" + pictures[i])
img = img.resize((150,150))
img.save("Validation/parrot/" + "picture" + str(i) + ".png")
i = i + 1
我嘗試使用 try except 方法,但它總是停止程序。 有沒有辦法使用 if 循環,比如“if imageisuseful() = false:”? 或者你可能有任何其他的想法? 感謝您的幫助。
如果你不希望你的try / except
停止你的代碼,你可以pass
:
try:
img = Image.open("downloads/parrot/" + pictures[i])
img = img.resize((150,150))
img.save("Validation/parrot/" + "picture" + str(i) + ".png")
i = i + 1
except:
pass # pictures[i] raised an exception here
根據錯誤發生的位置(在打開或調整大小時),您可能需要移動一些東西:
img = Image.open("downloads/parrot/" + pictures[i])
try:
img = img.resize((150,150))
img.save("Validation/parrot/" + "picture" + str(i) + ".png")
i = i + 1
except:
print("FILE {} DOESN'T WORK".format(pictures[i]))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.