How to put text in the input line: how to request user input on the command line, providing a "default" response that the user can edit or delete?

I am creating a Python script that requests input from the command line. The user will be able to edit part of the file. I can ask for new information and overwrite it in a file, no problem. But I would prefer that the editing part is already inserted into the command line, so it does not need to be typed completely. Is it 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 can be changed to

 >>>is not full either Edited file 

Afther for which the file was modified:

 1|This file 2|is not full either 

I hope it is clear what I am trying to accomplish.

This question was said to answer my question, it is to a certain extent. This happens when I run Linux with readline . However, I am not sure. I use Windows and do not use readline . I would like to use only the standard library.
An answer on Windows is also provided with this question. However, I get ImportError with win32console , perhaps because the question mentioned is not about Python3.4, but mine. Also, I was wondering if this is possible with a standard library , and not with an external library.

+13
python
May 12 '15 at 2:31
source share
3 answers

Unfortunately, I do not know if the input() type is available in the standard library with a default value.

There is an external solution - use win32console as indicated in this answer . As far as I can tell, he has two errors. First, imports come in the pywin32 package. So you would use pip install pywin32 , except that it does not work, due to the second trap: the package information in pypi is outdated, it says the package is incompatible with Python 3.4 ...

But actually it can work ! You should follow the “download URL” seen on the pypi project page (i.e. https://sourceforge.net/projects/pywin32/files/pywin32/ ) and install the latest version. I just installed build 219 for Py3.4, since I myself also use this version of Python. On page installers are provided for several versions of Python for 32-bit and 64-bit Windows.

Also, I changed the code from the above 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 your computer, can you provide an error message?

0
Aug 22 '15 at 12:42
source share

You can do this with tkinter:

 from tkinter import * def enter(): global commandEntry command = commandEntry.get() # Do stuff with command commandEntry.delete(0, END) def edit_line(line): global commandEntry commandEntry.insert(0, line) root = Tk() messageVar = StringVar() messageVar.set("Enter a command:") message = Label(root, textvariable=messageVar) commandEntry = Entry(root) enterButton = Button(root, text="Enter", command=enter) root.mainloop() 
0
Aug 24 '15 at 21:00
source share

You should have only two variables: one for the standard line, one for the line, which will be changed by the user independently. How:

 str1 = 'String that is standard' str2 = str1 #it usually will be standard string usr = input('your text goes here') if len(usr) != 0: str2 = usr #and here goes code for writing string into file 
-one
May 22 '15 at 12:01
source share



All Articles