# -*- coding:utf-8 -*-
import sys,cmd

class PyCDC(cmd.Cmd):
    def __init__(self):
        cmd.Cmd.__init__(self)
    def help_EOF(self):          ///help_和do_的标志要成对出现,help是提示帮助的内容,do是具体的执行代码
        print "Quits the program"
    def do_EOF(self,line):
        sys.exit()

    def help_walk(self):
        print "walk cd and export into *.cdc"
    def do_walk(self,filename):
        if filename == "":filename=raw_input("input the cdc`s name:")          ///弹出括号内的提示,输入的字符串保存为filename的值,值得注意的是该值只能在此处使用
        print "Walk the cd and save the infos to :'%s'" %filename

    def help_dir(self):
        print "This command is used to indentify the dir for saving/searching"
    def do_dir(self,pathname):
        if pathname=="": pathname=raw_input("Input the dir for saving/searching:" )
       

    def help_find(self):
        print "Search for the keyword"
    def do_find(self,keyword):
        if keyword == "": keyword=raw_input("Pls input the keyword: ")
        print "Searching for the keyword :'%s' " % keyword

if __name__=='__main__':
    cdc=PyCDC()
    cdc.cmdloop()   ///cmd命令调用,能够自动loop弹出cmd命令提示行

 

以下是几点确认:

1. if __name__=‘__main__': 是进行自测运行用的,具体怎么回事先不管,好用就成

2.class PyCDC(cmd.Cmd): 是进行类定义的,然后可以使用cdc=PyCDC()进行实体化,并使用类的各种功能。

3.def __init__(self): 是类实体化时自动运行的初始化函式。

4.cmd.Cmd.__init__(self)是初始化基类?不明白,反正要使用cmd模块,就这么来好了。

5.def help_*()和def do_*()应该成对出现,一个是打印功能的帮助信息,一个是实际干事儿的。

 

 

posted on 2010-02-04 19:36  猪总的小短裤  阅读(401)  评论(0编辑  收藏  举报