Hello everyone, I have searched for this high and low but could not find a solution.

To elaborate on the heading. I am trying to figure out a way to do this:

1. I have a text file with one line paths and file listing in it, for example:

E:\Movies\1408.avi
    E:\Movies\28.Days.Later[2002]
    E:\Movies\88Minutes-Gr8 Movie
    E:\Movies\Airplane.Part1![1980]

2. I would like to show one line at a time to the user on a command based program (windows cmd) and let them "EDIT" the 'pre-loaded' text and press enter (accept it in a variable as string).

3. Again as an example, I want the output to be something like this:

# Step 1 (Output)

The movie:
E:\Movies\28.Days.Later[2002] _  # <--  my cursor is the underscore.

#Step 2 (User edits the field)

The movie:
E:\Movies\28 Days Later _    # <-- after I finish editing the line.

# Step 3 (Press Enter)

The movie:

Thank you.

# done

I hope I have made it clear. I cannot find a way to pre-load the buffer with a string and display it to the user for editing and then accepting it.

Dani AI

Generated

This thread wants an editable, pre-filled line in a console prompt. pointed to low-level and library options and correctly flagged portability concerns. Two practical, modern choices are: a cross-platform line-editing library for simple prompts, or a terminal UI for richer interaction.

A simple, cross-platform solution is prompt_toolkit: it runs on Windows and Unix, gives full line-editing (history, keybindings) and accepts a default string that the user can edit. Example workflow:

from prompt_toolkit import prompt

with open('lines.txt') as fh:
    for raw in fh:
        default = raw.rstrip('\n')
        edited = prompt('The movie:\n', default=default)
        # edited now contains the user-edited string

Install with pip install prompt_toolkit. Docs: prompt_toolkit documentation.

On Unix-like systems the builtin readline can also prefill the input buffer by inserting text from a startup/pre-input hook (see readline.insert_text() and readline.redisplay() in the Python docs). This is lightweight but depends on GNU readline being available: readline — GNU readline interface.

For more complex needs (multi-field forms, navigation, full-screen editing) use a TUI toolkit such as curses/urwid/npyscreen. Note that curses is native to Unix; on Windows it needs ports/wrappers (or use prompt_toolkit instead). For Windows-only low-level control there are console APIs and packages (pyreadline exists but is less actively maintained), so prompt_toolkit is usually the simplest cross-platform choice. References: curses — Terminal handling for character-cell displays and pyreadline on PyPI.

Summary: for the OP scenario (read a line, show it prefilled and let it be edited), prompt_toolkit is the most straightforward, portable solution; use readline hooks only when targeting Unix/GNU readline, and switch to curses/other TUIs for full-screen editors.

Recommended Answers

All 2 Replies

How about using
http://docs.python.org/library/msvcrt.html

Keyboard reading and console printing functions.

Or maybe it is possible to use curses or readline routines?

Also if user has input things in keyboard they are in keyboard history and user can return to them by arrow keys and correct them.

@tonyvj

Thanks a lot for the information. I have some options to choose from now:
Still a long way before choosing the right way.

1. msvcrt: Only windows fx, I cannot port my app to Linux/mac

2. readline: Cannot use it in Windows without adding the package seperately.

3. curses: This seems useful.

I will post results after using all of them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.