将cmd命令提示融入到程序中,还未实现搜索部分

# _*_ coding: utf-8 _*_
import sys,cmd
from cdctools import *

class PyCDC(cmd.Cmd):
    def __init__(self):
        cmd.Cmd.__init__(self)
        self.CDROM='G:/'
        self.CDDIR='cdc/'
        self.prompt="(PyCDC)>-->>"
        self.intro='''PyCDC0.5 manual:
    dir  dir #used to save and search the dir ,default is "cdc"
    walk filename #used to traverse the cd infos,using ".cdc"
    find keyword #used to save and search all the .cdc files to output the file with the keyword in it
    ?    #query
    EOF  #exit the program
        '''
    def help_EOF(self):
        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:")
        print "Walk the cd and save the infos to :'%s'" %filename
        cdWalker(self.CDROM,self.CDDIR+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:" )
        self.CDDIR=pathname
        print "So the path for saving is : '%s'"%self.CDDIR
       

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

 

 

实现点:

1. from cdctools import * 可以引入已有脚本中所有函式

2. self. 在类声明中应该是代表自个儿,代表实体化后的那个自个儿。

3. 类的定量应该在__init__(self)初始化时声明。

4. cmd的prompt就是命令行环境的前缀字串。

5. cmd的intro 就是命令行环境的最初提示。

6. 程序中高亮成红色的部分通过修改为self.CDDIR=pathname+'/' 就可以不用加/

转载于:https://www.cnblogs.com/lumi/archive/2010/02/04/1663960.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值