標籤:python命令自動補全
環境介紹
OS:RHEL 5.5
Python版本:2.7.6
在使用linux命令的時候我們習慣使用下Tab鍵,在python下我們也可以實作類別似的功能。
類似Linix shell的TAB功能,Python也可以實現的!
具體實現過程如下:
1.編寫python指令碼
$ cat tab_enable.py
#!/usr/bin/python
# python startup file
import sys
import readline
import rlcompleter
import atexit
import os
# tab completion
readline.parse_and_bind(‘tab: complete‘)
# history file
histfile = os.path.join(os.environ[‘HOME‘], ‘.pythonhistory‘)
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter
2.呼叫指令碼
將指令碼放置在python預設模組,路徑下,
/usr/local/python27/lib/python2.7/tab_enable.py
開始調用
>>> import tab_enable
>>> import os
>>> os.<Tab>
備忘:
1.安裝readline時報錯gcc: readline/libreadline.a: No such file or directory,解決辦法
[[email protected] readline-6.2.4.1]#yum -y install readline-devel
[[email protected] readline-6.2.4.1]#yum -y install patch
[[email protected] readline-6.2.4.1]# python setup.py install
Python命令添加Tab鍵自動補全