python 命令行里实现clear清屏技巧


   对于不少新手,固然我也是新手,在命令行里学习python的时候占满了屏幕,很不习惯,特别是使用linux习惯了,使用clear清屏,这样的感受很是好,可是python下面没有这样的命令和功能,下面为了解决这个问题,本人写了个简单的模块
python


1 先来看下没有清屏的结果linux

[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.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>

这样特别让人厌恶,至少我有这样的感受bash


2 解决办法ide

[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  设置环境变量 spa


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()
>>>

 这里的效果贴出来不明显,你们动手试下,不懂的话能够在下面留言orm

相关文章
相关标签/搜索