为python命令行添加Tab键自动补全功能
在使用linux命令的时候我们习惯使用下Tab键,在python下我们也可以实现类似的功能。具体代码如下:
1$ cat startup.py
2#!/usr/bin/python
3# python startup file
4import sys
5import readline
6import rlcompleter
7import atexit
8import os
9# tab completion
10readline.parse_and_bind('tab: complete')
11# history file
12histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
13try:
14 readline.read_history_file(histfile)
15except IOError:
16 pass
17atexit.register(readline.write_history_file, histfile)
18del os, histfile, readline, rlcompleter
查看python默认的模块存放路径。拷贝功能脚本到默认模块存放路径: cp startup.py /usr/lib64/python2.x/
这时候可以通过以下方法进行测试:
1>>> import os
2>>> os.<Tab>
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/python-tab/4293.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.