Created
August 1, 2019 15:01
-
-
Save BBarbosa/3ee868e281b24c51b5fa7eb9c1f558bc to your computer and use it in GitHub Desktop.
Similiar implementation of windows timeout command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Python function to simulate Windows timeout command | |
NOTE: It may not work on Linux due to mvscrt library | |
""" | |
from msvcrt import getch, kbhit | |
from time import sleep | |
def timeout(seconds=0): | |
"""Similar Windows command line timeout implementation | |
Keyword Arguments: | |
seconds {int} -- Timeout lenght (default: {0}) | |
""" | |
if not isinstance(seconds, int) or seconds < 0: | |
raise ValueError("The timeout lenght should be an integer >= 0") | |
message = "\rWaiting for %d seconds, press any key to continue..." | |
n_prints = seconds + 1 | |
for i in range(n_prints): | |
if kbhit(): | |
getch() | |
break | |
print(message % (seconds - i), end="") | |
sleep(1) | |
if __name__ == "__main__": | |
timeout(12) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment