I am looking for a way to stop a script from running upon user input without getting cluttered exception output on screen and without reraising either.
I like the sys.exit()
approach, it achieves exactly what I want when running python3 myscript.py
, However this also kills my repl if I am running the function interactively, and this is what I want to avoid.
An example would be:
def wait_input(message,timeout):
print (message)
i, o, e = select.select( [sys.stdin], [], [], timeout )
if (i):
return sys.stdin.readline().strip()
else:
return ""
def user_checkpoint(question, accepted_answers=("y","yes") ):
answer = wait_input(question,3600).lower()
if answer not in accepted_answers:
print('Abort')
somethinglike.sys.exit()
> user_checkpoint('keep running?')
n
Abort
>
important: note that I am still in repl after Abort, which does not happen if I actually do use sys.exit() in the code.