tags:

views:

208

answers:

4

Is it possible for python to accept input like this:

Folder name: Download

But instead of the user typing "Download" it is already there as a initial value. If the user wants to edit it as "Downloads" all he has to do is add a 's' and press enter.

Using normal input command:

folder=input('Folder name: ')

all I can get is a blank prompt:

Folder name:

Is there a simple way to do this that I'm missing?

+3  A: 

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.

rlotun
+5  A: 

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:

def rlinput(prompt, prefill=''):
   readline.set_startup_hook(lambda: readline.insert_text(prefill))
   try:
      return raw_input(prompt)
   finally:
      readline.set_startup_hook()
sth
Thank you for you reply, this is what I needed.
kircheis
A: 

If you do that, the user would have to delete the existing word. What about providing a default value if the user hits "return"?

>>> default_folder = "My Documents"
>>> try: folder = input("folder name [%s]:" %default_folder)
... except SyntaxError: folder = default_folder
Stephen
A: 

I think that the best (the easiest and most portable) solution is a combination of @rlotun and @Stephen answers:

default = '/default/path/'
dir = raw_input('Folder [%s]' % default)
dir = dir or default
paffnucy