Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Python»How to Use Sleep in Python Script

    How to Use Sleep in Python Script

    By RahulJanuary 11, 20191 Min Read

    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.

    Advertisement

    You can use time.sleep(seconds) as per following. The value passed to sleep() function is in second.

    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
    

    Python Sleep
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Create and Read List in Python

    What are the Python Static Variables

    Python Program to Add Two Numbers

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.