Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting raw_input() with pre determined text

I'd like to be able to get input from the user (through raw_input() or a module) and be able to have text automatically be already entered that they can add to, delete, or modify. I know in javascript when you're using a prompt, you can do it like

var = prompt("Enter your name: ","put name here")

and it will appear as:

Enter your name:
put name here

where 'put name here' is in the text box and can be modified. I'm hoping to implement this in a shell environment (I use unix) as opposed to a window.

Any ways to do this?

Oh and tell me if I need to clarify what I am hoping for more.


I don't think you guys understand what I'm trying to do.

I basically want to include the prompt in the input, but have the user be able to backspace out the prompt/edit it if they want.

The script would possibly be used for a simple shell based one line text editor, and a tab completion type thing.

like image 613
Brennan Wilkes Avatar asked Sep 29 '13 23:09

Brennan Wilkes


People also ask

What is raw_input () in Python give an example?

a = input() will take the user input and put it in the correct type. Eg: if user types 5 then the value in a is integer 5. a = raw_input() will take the user input and put it as a string. Eg: if user types 5 then the value in a is string '5' and not an integer.

What does the Python raw_input () function do?

Python raw_input function is used to get the values from the user. We call this function to tell the program to stop and wait for the user to input the values. It is a built-in function.

Can we use raw_input in Python 3?

The raw_input() function reads a line from input (i.e. the user) and returns a string by stripping a trailing newline. This page shows some common and useful raw_input() examples for new users. Please note that raw_input() was renamed to input() in Python version 3.


2 Answers

On UNIX and UNIX-alikes, such as Mac OS X and Linux, you can use the readline module. On Windows you can use pyreadline.

If you do it the way minitech suggests in his comment, write a little function to make it easier:

def input_default(prompt, default):
    return raw_input("%s [%s] " % (prompt, default)) or default

name = input_default("What is your name?", "Not Sure")
like image 171
kindall Avatar answered Oct 03 '22 14:10

kindall


Mmm, kinda hack, but try this one.

Windows:

import win32com.client as win

shell = win.Dispatch("WScript.Shell").SendKeys("Put name here")
raw_input("Enter your name: ")

In Linux/Unix environment, you can use the pyreadline, readline or curses libraries. You can find one possible solution here:

def rlinput(prompt, prefill=''):
    readline.set_startup_hook(lambda: readline.insert_text(prefill))
    try:
        return raw_input(prompt)
    finally:
        readline.set_startup_hook()
like image 24
Yam Mesicka Avatar answered Oct 03 '22 12:10

Yam Mesicka