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?
The standard library functions
input()
andraw_input()
don't have this functionality. If you're using Linux you can use thereadline
module to define an input function that uses a prefill value and advanced line editing:If you do that, the user would have to delete the existing word. What about providing a default value if the user hits "return"?
I think that the best (the easiest and most portable) solution is a combination of @rlotun and @Stephen answers:
I finally found a simple alternative that works on Windows and Linux. Essentially, i'm using the pyautogui module to simulate the user's input. in praxis, that looks like this:
A Word of Warning:
typewrite
finishes.No module named 'Xlib'
, try to install thepython3-xlib
orpython-xlib
package (or thexlib
module). Running over ssh can also be a problem.An example fallback implementation:
Since a missing x-server can logically only happen on linux, here's an implementation that uses sth's answer as fallback:
This works in windows.
Not the best aproach but for the sake of sharing... You could use Javascript to get all sort of inputs in IPython Notebook.
On the next cell you can use the new variable: