1. 程式人生 > >Python命令列自動補全和記錄歷史命令

Python命令列自動補全和記錄歷史命令

[root@mm_local ~]# echo "export PYTHONSTARTUP='/root/.pythonstartup'" >> /root/.bashrc 
[root@mm_local ~]# source /root/.bashrc
[root@mm_local ~]# cat /root/.pythonstartup 
import os
import readline
import rlcompleter
import atexit


#tab completion
readline.parse_and_bind("tab: complete")


#history file
history_file = os.path.join(os.environ["HOME"],".pythonhistory")
try:
    readline.read_history_file(history_file)
except IOError:
    pass
atexit.register(readline.write_history_file,history_file)


del os,history_file,readline,rlcompleter
[root@mm_local ~]# python
Python 2.7.3 (default, Jun  5 2013, 22:40:26) 
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path<tab> #按tab鍵就可以自動補全了

[root@mm_local ~]# python
>>>
>>> sys.path #按上下方向鍵即可找到歷史命令