You need to import and use a module called time. This module provides various time-related functions for Python users.
Python sleep syntax
The syntax is as follows
The time.sleep() suspend execution of a script for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.
Example: Put delay in a Python script
Create a script called demo-sleep.py:
#!/usr/bin/python # The following will run infinity times on screen till user hit CTRL+C # The program will sleep for 1 second before updating date and time again. import time print "*** Hit CTRL+C to stop ***" ## Star loop ## while True: ### Show today's date and time ## print "Current date & time " + time.strftime("%c") #### Delay for 1 seconds #### time.sleep(1)
Save and close the file. Run it as follows:
$ chmod +x demo-sleep.py