python 命令行裏實現clear清屏技巧


   對於很多新手,當然我也是新手,在命令行裏學習python的時候佔滿了屏幕,很不習慣,特別是使用linux習慣了,使用clear清屏,這樣的感覺非常好,但是python下面沒有這樣的命令和功能,下面爲了解決這個問題,本人寫了個簡單的模塊


1 先來看下沒有清屏的結果

[root@zh ~]# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:11:10)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>

這樣特別讓人厭惡,至少我有這樣的感覺


2 解決辦法

[root@zh ~]# cat clear.py
#!/usr/bin/python
import os
def clear():
    os.system('clear')



3 爲了更使用方便,我們不用每次都使用import導入


[root@zh ~]# cat startup.py
#!/usr/bin/python
# python startup file
                                                                                                   
import sys
import readline
import rlcompleter
import atexit
import os
import clear
# 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

  大家留意看下import clear   我上面這個腳本是python的tab功能


4  設置環境變量


del os, histfile, readline, rlcompleter
[root@zh ~]# cat .bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
export PYTHONSTARTUP=/root/startup.py
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
[root@zh ~]# source .bashrc
[root@zh ~]#



5  測試


[root@zh ~]# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:11:10)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> clear.clear()
>>>

 這裏的效果貼出來不明顯,大家動手試下,不懂的話可以在下面留言



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章