Python实现Tab自动补全和历史命令管理的方法,pythontab


本文实例讲述了Python实现Tab自动补全和历史命令管理的方法。分享给大家供大家参考。具体分析如下:

Python的startup文件,即环境变量 PYTHONSTARTUP 对应的文件

1. 为readline添加tab键自动补全的功能

2. 像Shell一样管理历史命令

代码如下:
复制代码 代码如下:import rlcompleter
import readline
import atexit
import os
# http://stackoverflow.com/questions/7116038/python-tab-completion-mac-osx-10-7-lion
if 'libedit' in readline.__doc__:
    readline.parse_and_bind('bind ^I rl_complete')
else:
    readline.parse_and_bind('tab: complete')
histfile = os.path.join(os.environ['HOME'], '.pyhist')
try:
    readline.read_history_file(histfile)
except IOError:
    pass
atexit.register(readline.write_history_file, histfile)
del readline, rlcompleter, histfile, os

希望本文所述对大家的Python程序设计有所帮助。

评论关闭