FurtiveFelon FurtiveFelon - 1 year ago 79
Python Question

How to retry after exception in python?

I have a loop starting with

for i in range(0, 100)
. Normally it runs correctly, but sometimes it fails due to network conditions. Currently I have it set so that on failure, it will
in the except clause (continue on to the next number for

Is it possible for me to reassign the same number to
and run through the failed iteration of the loop again?

Answer Source

Do a while True inside your for loop, put your try code inside, and break from that while loop only when your code succeeds.

for i in range(0,100):
    while True:
            # do stuff
        except SomeSpecificException: