Skip to content

How to Use Sleep in Python Script

Category
Python
Versions & support
About Python →

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.

Python Delay Example

Let’s create a small script to print current date time every after 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

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top