用python的cmd模块写一个简单的shell
from cmd import *
class MyShell(Cmd):
def preloop(self):
print "print this line before entering the loop"
def postloop(self):
print "print this line after leaving the loop"
def precmd(self, line):
print "print this line before do a command"
return Cmd.precmd(self, line)
def postcmd(self, stop, line):
print "print this line after do a command"
return Cmd.postcmd(self, stop, line)
def do_test(self, line):
''' test command, just print the arguments except some special ones'''
if line == "exit":
exit()
else:
print line
MyShell().cmdloop()
#继承cmd模块的Cmd类实现
#shell中的命令xx在类中对应的函数名为do_xx
#line是shell中输入的命令行参数
相关文档:
在Python中,我们执行表达式 a = 3,Python会怎样操作呢?
1、首先会创建一个对象表示值3
2、如果变量a不存在,创建变量a
3、把变量a连接到表示3的对象
在Python中,变量和对象存储在不同的地方,通过指针连接起来...
一个变量总是和对象连接起来,不存在不和对象连接的变量,但是大的对象可能连接到别的对象。 ......
#!/usr/bin/env python
# -*-coding:UTF-8-*-#这一句告诉python用UTF-8编码
#=========================================================================
#
# NAME: Python MySQL test
#
# AUTHOR: benyur
# DATE : 2004-12-28
#
# COMMENT: 这是一个python连接mysql的例子
#
#================ ......
ipython 如果没有用装, 那就赶紧装上, 这个东西比起python自己带的那个交互界面要好用很多
shutil 类似于shell的一些接口, 比如 cp, mv等等
subprocess 调用子进程
optparse 解析命令行参数的, 用它来应付命令行参数, 简洁, 清晰
sqlite3 数据库, 进程级的数据库, 很酷, 甚至可以把这个数据库建在内存里. http://www.py ......