Show default value for editing on Python input possible? Show default value for editing on Python input possible? python python

Show default value for editing on Python input possible?


The standard library functions input() and raw_input() don't have this functionality. If you're using Linux you can use the readline module to define an input function that uses a prefill value and advanced line editing:

import readlinedef rlinput(prompt, prefill=''):   readline.set_startup_hook(lambda: readline.insert_text(prefill))   try:      return input(prompt)  # or raw_input in Python 2   finally:      readline.set_startup_hook()


I'm assuming you mean from the command-line. I've never seen initial values for command line prompts, they're usually of the form:

Folder [default] : 

which in code is simply:

res = raw_input('Folder [default] : ')res = res or 'default'

Alternatively, you can try to do something using the curses module in Python.


This works in windows.

import win32console_stdin = win32console.GetStdHandle(win32console.STD_INPUT_HANDLE)def input_def(prompt, default=''):    keys = []    for c in unicode(default):        evt = win32console.PyINPUT_RECORDType(win32console.KEY_EVENT)        evt.Char = c        evt.RepeatCount = 1        evt.KeyDown = True        keys.append(evt)    _stdin.WriteConsoleInput(keys)    return raw_input(prompt)if __name__ == '__main__':    name = input_def('Folder name: ')    print    print name