0
    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)

1
  • 4
    for f in file_list: try: return load(f) except ...…? Commented Jun 24, 2021 at 14:03

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.