I have this simple loop in Python that I am using..
for i, numbercode in enumerate(numbercode_list):
print ("Item : %s" %i )
driver.close()
driver = init_driver()
How would I go about just running the last two lines every 100th loop item? Currently it runs them every time.
The time module sleep() function allows you to sleep your code for a defined period of time in seconds. To run code every 5 seconds in Python, you can use a loop and pass '5' for 5 seconds to sleep().
So it will iterate for 10 times.
You can take the modulus of the number and compare to zero. Modulo (%
in Python) yields the remainder from the division of the first argument by the second, e.g.
>>> 101 % 100
1
>>> 100 % 100
0
For your example, this could be applied as follows:
for i, numbercode in enumerate(numbercode_list):
print ("Item : %s" %i )
if i % 100 == 0:
driver.close()
driver = init_driver()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With