Is it possible to show the default value for editing in Python input?

Is it possible for python to accept input as follows:

 Folder name: Download

But instead of typing “Download,” the user already exists as the initial value. If the user wants to edit it as “Downloads”, all he needs to do is add 's' and press enter.

Using the normal input command:

folder=input('Folder name: ') 

all i can get is an empty prompt:

 Folder name:

Is there an easy way to do this that I am missing?

+44
python input
Mar 28 '10 at 13:51
source share
7 answers

The standard library functions input() and raw_input() do not have this function. If you are using Linux, you can use the readline module to define an input function that uses the pre-fill 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() 
+52
Mar 28 '10 at 14:01
source share
— -

I assume you mean from the command line. I never saw the initial values ​​for the command line, they usually have the form:

  Folder [default] : 

which in the code is simple:

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

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

+15
Mar 28
source share

It 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 
+9
May 4 '11 at 18:33
source share

I think the best (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 
+2
Mar 28
source share

Not the best aspect, but for sharing ... You can use Javascript to get all kinds of IPython Notebook logins.

 from IPython.display import HTML newvar = "" htm = """ <input id="inptval" style="width:60%;" type="text" value="This is an editable default value."> <button onclick="set_value()" style="width:20%;">OK</button> <script type="text/Javascript"> function set_value(){ var input_value = document.getElementById('inptval').value; var command = "newvar = '" + input_value + "'"; var kernel = IPython.notebook.kernel; kernel.execute(command); } </script> """ HTML(htm) 

In the following cell, you can use the new variable:

 print newvar 
0
Mar 16 '14 at 5:22
source share

This is not a good answer, but it is a job for windows. As far as I tried, I could not get Readline or pyReadline to work on my Windows 10 machine with Python Ver 3.5. So I wrote this instead. Not the best code in the world since I only used Python for 3 months. But it works.

  import os def note_input(defaultvalue): #Create a textfile txtfile = open("txtfile.txt", "w") # # populate it with the default value txtfile.write(defaultvalue) txtfile.close() # # call Notepad os.system("notepad.exe txtfile.txt") # input("Just holding until notepad is close : ") (did not need this line) # # get the Value Entered/Changed in Notepad txtfile = open("txtfile.txt", "r") func_value = txtfile.read() txtfile.close() return func_value # END DEF 

Notepad stopped the program from starting until it was closed, so the input line () below it is not needed. As soon as the notebook was opened for the first time and placed where I wanted it on the screen, it looked like an input popup. I assume that you can use any text editor such as Notepad ++ or Scripe or Code Writer, etc.

-one
Mar 22 '17 at 3:04 on
source share

If you do this, the user will have to delete the existing word. How about providing a default if the user presses "return"?

 >>> default_folder = "My Documents" >>> try: folder = input("folder name [%s]:" %default_folder) ... except SyntaxError: folder = default_folder 
-3
Mar 28 '10 at 14:01
source share



All Articles