Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delay between for loop iteration (python)

Tags:

python

loops

web

Is this possible in Python? I wrote a great loop/script in Python and I’d like to add this delay to it if at all possible.

map(firefox.open, ['http://www.bing.com/search?q=' + str(i) for i in range(x))], [2] * x)

Where shall I put the sleep(6) in this?

like image 943
user3072758 Avatar asked Apr 21 '14 13:04

user3072758


2 Answers

You can do that using time.sleep(some_seconds).

from time import sleep

for i in range(10):
    print i
    sleep(0.5)    #in seconds

Implementation

Here is a cool little implementation of that: (Paste it in a .py file and run it)

from time import sleep

for i in range(101):
    print '\r'+str(i)+'% completed',
    time.sleep(0.1)

map(firefox.open, ['http://www.bing.com/search?q=' + str(i) for i in range(x))], [2] * x)

like image 158
sshashank124 Avatar answered Oct 16 '22 17:10

sshashank124


Or if you want it to start on one and emulate a stopwatch:

import time
def count_to(number):
    for i in range(number):
        time.sleep(1)
        i += 1
        if i >= number:
            print('Time is up')
            break
    print(i)
like image 23
Wool Avatar answered Oct 16 '22 15:10

Wool