Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put text in input line: how to ask for user input on the command line while providing a 'default' answer that the user can edit or delete?

I am creating a Python script that asks for input from the command line. The user will have the ability to edit a part of a file. I can ask for the new information and overwrite it in the file, no problem. But I would rather have the to-edit part of the file already put in the command line, so it does not have to be typed completely over. Is this possible?

File:

1|This file
2|is not empty

Example:

>>>edit line 2
Fetching line 2
Edit the line then hit enter
>>>is not empty                  #This is written here by the script, not by the user

Which then can be changed to

>>>is not full either
Edited file

Afther which the file has changed to:

1|This file
2|is not full either

I hope it's clear what I am trying to accomplish.

This question has been said to answer my question, it does to a certain extent. It does when I am running Linux with readline. However, I am not. I am using Windows and am not using readline. I would like to only use the standard library.
An answer for Windows is also provided with that question. However, I get an ImportError with win32console, it might be because mentioned question is not about Python3.4, but mine is. Also, I was wondering if this was possible with the standard library, not with an external library.

like image 713
ikhebgeenaccount Avatar asked May 12 '15 14:05

ikhebgeenaccount


People also ask

How do you ask for user input in Python?

In Python, we can get user input like this: name = input("Enter your name: ") print("Hello", name + "!") The code above simply prompts the user for information, and the prints out what they entered in.

What function is used to get user input from the command line?

scanf() function is used to read input from the console or standard input of the application in C and C++ programming language. scanf() function can read different data types and assign the data into different variable types.

Which command should you use to prompt the user for input?

The prompt() method displays a dialog box that prompts the user for input. The prompt() method returns the input value if the user clicks "OK", otherwise it returns null .


1 Answers

Unfortunately, I don't know if kind of input() with default value is available in standard library.

There is an external solution - use win32console as mentioned in this answer. However, it has two pitfalls as far as I can see. First, the import is bundled in a package pywin32. So you would use pip install pywin32, except it does not work, because of the second pitfall: the information about the package at pypi is outdated, it says that package is incompatible with Python 3.4...

But in fact, it can work! You should follow the "Download URL" visible at pypi project page (i.e. https://sourceforge.net/projects/pywin32/files/pywin32/ ) and install latest build. I just installed build 219 for Py3.4, as I myself also use this Python version. On the page installers are provided for several Python versions for 32bit and 64bit Windows.

Also, I've tweaked the code from above-linked SO answer to work in Python 3:

import win32console

_stdin = win32console.GetStdHandle(win32console.STD_INPUT_HANDLE)

def input_def(prompt, default=''):
    keys = []
    for c in str(default):
        evt = win32console.PyINPUT_RECORDType(win32console.KEY_EVENT)
        evt.Char = c
        evt.RepeatCount = 1
        evt.KeyDown = True
        keys.append(evt)

    _stdin.WriteConsoleInput(keys)
    return input(prompt)

if __name__ == '__main__':
    name = input_def('Folder name: ', 'it works!!!')
    print()
    print(name)

This works on my Windows machine... If this does not work on yours, can you provide the error message?

like image 78
mbdevpl Avatar answered Oct 22 '22 04:10

mbdevpl