LoginSignup
1
2

More than 3 years have passed since last update.

pythonでinput()にいい感じで補完を効かせたい

Posted at

readlineで input() (python2で raw_input() だったやつ) のときにreadline module (GNU readlineじゃなくて) を使うとtabでcompletionできていい感じになるんですけど余りにも情報がないので困った話

フォルダとかのcompletionとかだとまぁ適当にやればよさそうなんだけど、なんか任意のlistでやらせたい場合は以下の内容が参考になるようす
https://stackoverflow.com/questions/18369207/python-user-input-replay

要するに、Completerクラスをでっち上げて

readline.parse_and_bind("tab: complete")
readline.set_completer(completer.complete)

みたいなことするとinput() したときにtabで補完できるようになるっぽい

Windows使ってるなら

$ pip install pyreadline

すればいい感じっぽい (import pyreadline しなくても import readline で動くのでクロスプラットフォームでうれしい、少なくとも私の環境では動いた)

もうちょい複雑な例: https://stackoverflow.com/questions/5637124/tab-completion-in-pythons-raw-input

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2