【命令行运行python】为python命令行添加Tab键自动补全功能

更新时间:2019-10-21    来源:python    手机版     字体:

【www.bbyears.com--python】

在使用linux命令的时候我们习惯使用下Tab键,在python下我们也可以实现类似的功能。具体代码如下:

$ cat  startup.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
查看python默认的模块存放路径。拷贝功能脚本到默认模块存放路径:

cp startup.py /usr/lib64/python2.x/

这时候可以通过以下方法进行测试:

>>> import os
>>> os.

本文来源:http://www.bbyears.com/jiaocheng/74538.html

热门标签

更多>>

本类排行