用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中输入的命令行参数
相关文档:
3. Dictionaries 字典类型
Python中,字典类型并不是顺序容器,而类似c++中的关联容器(map),Dictionaries中存储的是键/值 对,和map不同的是,Python的Dictionaries中可以存任意对象类型。Dictionaries类型也是可变的,和Lists一样,可以原地修改(通过下标修改)。
下面通 ......
4、Tuples 元组
元组和Lists相似,但它是immutable,初始化后不能改变其内容,这在程序中有时候很有用,可以用来防止定义的变量内容被意外改变。
5、Files 文件
文件操作和c语言比较接近,下面只通过代码演示:
>>> f = open('data.txt','w')
>>> ......
Ctrl+3 行注释
Ctr+\ 去行注释
Ctrl+Shift+3 去行注释
Ctrl+4 块注释
Ctrl+5 & ......
转自 http://hi.baidu.com/xunxun129/blog/item/3befad0f8ff992c07bcbe180.html
有时我们需要在 Python 中使用 zip 文件,而在1.6版中,Python 就已经提供了 zipfile 模块可以进行这样的操作。不过 Python 中的 zipfile 模块不能处理多卷的情况,不过这种情况并不多见,因此在通常情况下已经足够使用了。下面我只是对 ......
什么是pyc文件
pyc是一种二进制文件,是由py文件经过编译后,生成的文件,是一种byte code,py文件变成pyc文件后,加载的速度有所提高,而且pyc是一种跨平台的字节码,是由python的虚拟机来执行的,这个是类似于JAVA或者.NET的虚拟机的概念。pyc的内容,是跟python的版本相关的,不同版本编译后的pyc文件是不同的,2 ......