用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')
>>> ......
1 你好
#打开新窗口,输入:
#! /usr/bin/python
# -*- coding: utf8 -*-
s1=input("Input your name:")
print("你好,%s" % s1)
'''
知识点:
* input("某字符串")函数:显示"某字符串",并等待用户输入.
......
#!/usr/bin/env python
# -*-coding:UTF-8-*-#这一句告诉python用UTF-8编码
#=========================================================================
#
# NAME: Python MySQL test
#
# AUTHOR: benyur
# DATE : 2004-12-28
#
# COMMENT: 这是一个python连接mysql的例子
#
#================ ......
Ctrl+3 行注释
Ctr+\ 去行注释
Ctrl+Shift+3 去行注释
Ctrl+4 块注释
Ctrl+5 & ......