In Python programming function you can use sleep() function available under time module. This will sleep or delay script execution for the defined number of seconds.
You can use time.sleep(seconds) as per following. The value passed to sleep() function is in second.
Advertisement
1 2 3 4 5 | import time time.sleep(1) # delays for 1 second time.sleep(5) # delays for 5 seconds time.sleep(60) # delays for 1 minute |
Python Delay Example
Let’s create a small script to print current date time every after 5 seconds.
1 2 3 4 5 | import time while True: print("DateTime " + time.strftime("%c")) time.sleep(5) # delays execution by 5 seconds |
Execute above script on command line. To exit running script press CTRL + C. You will see results something like below
DateTime Fri Sep 28 17:39:50 2018 DateTime Fri Sep 28 17:39:55 2018 DateTime Fri Sep 28 17:40:00 2018 DateTime Fri Sep 28 17:40:05 2018 DateTime Fri Sep 28 17:40:10 2018