Reputation: 63
try:
return load(file)
except FileNotFoundError:
try:
return load(otherfile)
except FileNotFoundError:
return None
How to write it more clean ? I feel there must be a better way to acomplish this (I assume there will be third file to load when other two will fail)
Upvotes: 0
Views: 114
Reputation: 131
You can put it into the loop if you will have many files:
file_list = [file, otherfile]
for f in file_list:
try:
return load(f)
except FileNotFoundError:
continue
return None
Upvotes: 3