Baffled by readline module

Grant Edwards grant.b.edwards at gmail.com
Thu Mar 9 14:50:52 EST 2023


On 2023-03-09, Grant Edwards <grant.b.edwards at gmail.com> wrote:

> In an interactive command-line Python program on Linux, I want to be
> able to read a line at a time from stdin, providing command line
> history and editing to the user. In C, I would use GNU readline to do
> that.
>
> Python has the readline module, which appears to be a wrapper for GNU
> readline. However, I've read and re-read the documenation for that
> module, but I'm completely baffled.

After googling for a while, I finally stumbled across an old Python 2
example that pointed me in the right direction. Here's the sort of
example I had hoped to find in the module's docs:

#!/usr/bin/python
import readline

readline.parse_and_bind('set editing-mode emacs')

while True:
    try:
        line = input('enter something (EOF to quit): ')
    except EOFError:
        print()
        break
    print('You entered: "%s"' % line)






More information about the Python-list mailing list