How do I check if raw input is integer in python 2.7?

isinstance(raw_input(“number: “)), int) always yields False because raw_input return string object as a result. Use try: int(…) … except ValueError: number = raw_input(“number: “) try: int(number) except ValueError: print False else: print True or use str.isdigit: print raw_input(“number: “).isdigit() NOTE The second one yields False for -4 because it contains non-digits character. Use the second … Read more

Tab completion in Python’s raw_input()

Here is a quick example of how to perform incremental completion of file system paths. I’ve modified your example, organizing it into a class where methods named complete_[name] indicate top-level commands. I’ve switched the completion function to use the internal readline buffer to determine the state of the overall completion, which makes the state logic … Read more

Masking user input in python with asterisks

If you want a solution that works on Windows/macOS/Linux and on Python 2 & 3, you can install the pwinput module: pip install pwinput Unlike getpass.getpass() (which is in the Python Standard Library), the pwinput module can display *** mask characters as you type. Example usage: >>> pwinput.pwinput() Password: ********* ‘swordfish’ >>> pwinput.pwinput(mask=’X’) # Change … Read more

raw_input and timeout [duplicate]

There’s an easy solution that doesn’t use threads (at least not explicitly): use select to know when there’s something to be read from stdin: import sys from select import select timeout = 10 print “Enter something:”, rlist, _, _ = select([sys.stdin], [], [], timeout) if rlist: s = sys.stdin.readline() print s else: print “No input. … Read more

How to set time limit on raw_input

The signal.alarm function, on which @jer’s recommended solution is based, is unfortunately Unix-only. If you need a cross-platform or Windows-specific solution, you can base it on threading.Timer instead, using thread.interrupt_main to send a KeyboardInterrupt to the main thread from the timer thread. I.e.: import thread import threading def raw_input_with_timeout(prompt, timeout=30.0): print(prompt, end=’ ‘) timer = … Read more