Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to prefill a input() in Python 3's Command Line Interface?

I'm using Python 3.2 on Ubuntu 11.10 (Linux). A piece of my new code looks like this:

text = input("TEXT=")

Is it possible to get some predefined string after the prompt, so I can adjust it if needed? It should be like this:

python3 file TEXT=thepredefinedtextishere 

Now I press Backspace 3 times

TEXT=thepredefinedtextish 

Now I press Enter, and the variable text should be thepredefinedtextish

like image 409
Exeleration-G Avatar asked Dec 14 '11 13:12

Exeleration-G


People also ask

Is input () supported in Python 3?

Python 3 – input() function. In Python, we use input() function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function convert it into a string.

How does Python get input from the command line?

Python provides developers with built-in functions that can be used to get input directly from users and interact with them using the command line (or shell as it is often called). In Python 2, raw_input() and in Python 3, we use input() function to take input from Command line.

What is the input command for Python?

Input using the input( ) function Python has an input function which lets you ask a user for some text input. You call this function to tell the program to stop and wait for the user to key in the data. In Python 2, you have a built-in function raw_input() , whereas in Python 3, you have input() .


1 Answers

If your Python interpreter is linked against GNU readline, input() will use it. In this case, the following should work:

import readline  def input_with_prefill(prompt, text):     def hook():         readline.insert_text(text)         readline.redisplay()     readline.set_pre_input_hook(hook)     result = input(prompt)     readline.set_pre_input_hook()     return result 
like image 127
Sven Marnach Avatar answered Sep 28 '22 14:09

Sven Marnach