Is it possible to prefill a input() in Python 3's Command Line Interface?

前端 未结 1 1931
温柔的废话
温柔的废话 2020-12-16 09:44

I\'m using Python 3.2 on Ubuntu 11.10 (Linux). A piece of my new code looks like this:

text = input(\"TEXT=\")

Is it possible to get some predef

1条回答
  •  有刺的猬
    2020-12-16 10:29

    If your Python interpreter is linked against GNU readline, input() will use it. In this case, the following should work:

    import readline
    
    def input_with_prefill(prompt, text):
        def hook():
            readline.insert_text(text)
            readline.redisplay()
        readline.set_pre_input_hook(hook)
        result = input(prompt)
        readline.set_pre_input_hook()
        return result
    

    0 讨论(0)
提交回复
热议问题