Environment Introduction
Os:rhel 5.5
Python version: 2.7.6
We are used to using the TAB key when using Linux commands, and we can implement similar functions under Python.
Like the tab function of the Linix shell, Python can also be implemented!
The specific implementation process is as follows:
1. Writing a Python script
$ 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. Calling scripts
Place the script in the Python default module, under the path,
/usr/local/python27/lib/python2.7/tab_enable.py
Start calling
>>> Import tab_enable
>>> Import OS
>>> os.<tab>
Note:
1. Install readline times wrong gcc:readline/libreadline.a:no such file or directory, workaround
[[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 command to add Tab key Auto-complete