I have to ask the user for 2 inputs. The first one is an integer to be used here.
workbook = xlsxwriter.Workbook(str(date.today() - timedelta(days=days)) + '-' + excel_name + '.xlsx')
so,
days = int(input("Enter the number of days :")
wait for say, 10 seconds..if nothing is entered, make the value of days to 1. Same thing for the next option. User needs to select from a list of choice, if nothing is entered for 10 seconds, choose the 1st option.
excel_name = ['a','b','c','d']
for i in range(len(excel_name)):
print(i, ".", excel_name[i])
choice = int(input("Enter the number of Menu Choice ")
I tried this..this always reaches timeout and doesn't care about what is entered.
from inputimeout import inputimeout, TimeoutOccurred
try:
something = inputimeout(prompt='>>', timeout=5)
except TimeoutOccurred:
something = 'something'
print(something)
Any ideas?
As someone pointed out..This should work..
import msvcrt
import time
t0 = time.time()
while time.time() - t0 < 600:
if msvcrt.kbhit():
if msvcrt.getch() == '\r': # not '\n'
break
time.sleep(0.1)
EDIT :- As suggested, I tried one of the linked answers,
class TimeoutExpired(Exception):
pass
def input_with_timeout(prompt, timeout, timer=time.monotonic):
sys.stdout.write(prompt)
sys.stdout.flush()
endtime = timer() + timeout
result = []
while timer() < endtime:
if msvcrt.kbhit():
result.append(msvcrt.getwche()) #XXX can it block on multibyte characters?
if result[-1] == '\r':
return ''.join(result[:-1])
time.sleep(0.04) # just to yield to other processes/threads
raise TimeoutExpired
It reaches timeout
and ignores the input.