CDays–2 完成核心功能 CMD模塊 Python基礎教程 cmd cli


再過兩個CDays我們就完成了所有的功能了,不過是在CMD中運行的。

為了模塊化我們的程序,我們先整理一下以前的程序。

# -*- coding: utf-8 -*-
import os
def cdWalker(cdrom,cdcfile):
    export = ""
    for root, dirs, files in os.walk(cdrom):
        export+="\n %s;%s;%s" % (root,dirs,files)
    open(cdcfile, 'w').write(export)

if __name__ == '__main__':      # this way the module can be
    CDROM = 'D:\\CDROM'

這個模塊完成了CDROM的遍歷,並存進了指定文件中。

雖然這個程序沒有GUI,那么讓我們給他設計一個CMD運行的界面吧。

根據上一次的日志,我們發現根據命令不同會有很多很多的分支,那么我們看一下書上給的例子pycdc-v0.4.py。

# -*- coding: utf-8 -*-
'''pycdc-v0.4.py
Lovely Python -2 PyDay 
'''
import sys, cmd
class PyCDC(cmd.Cmd):
    def __init__(self):
        cmd.Cmd.__init__(self)                # initialize the base class
        self.CDROM = 'D:\\CDROM'
        self.CDDIR = 'D:\\'

    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("輸入cdc文件名:: ")
        print "掃描光盤內容保存到:'%s'" % filename

    def help_dir(self):
        print "指定保存/搜索目錄"
    def do_dir(self, pathname):
        if pathname == "": pathname = raw_input("輸入指定保存/搜索目錄: ")
        print "指定保存/搜索目錄:'%s' ;默認是:'%s'" % (pathname,self.CDDIR)

    def help_find(self):
        print "搜索關鍵詞"
    def do_find(self, keyword):
        if keyword == "": keyword = raw_input("輸入搜索關鍵字: ")
        print "搜索關鍵詞:'%s'" % keyword

if __name__ == '__main__':      # this way the module can be
    cdc = PyCDC()            # imported by other programs as well
    cdc.cmdloop()

根據書上所述,我們不知道上面的一些語法是什么,的確,我們確實不知道。

讓我們運行一下.

image

提示輸入.

image

輸入非法字符有提示.

image

輸入合法字符, 好吧,程序里沒有,我承認我已經看暈了.

讓我們看一下書,書上說是用的CMD模塊, 查一下cmd是干嘛的.

根據作弊條PCS201

cmd模塊為命令行接口cli提供了一個簡單的框架.下面給出了一個例子.

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

class CLI(cmd.Cmd):

    def __init__(self):
        cmd.Cmd.__init__(self)
        self.prompt = '> '    # 定義命令行提示符

    def do_hello(self, arg):   # 定義hello命令所執行的操作
        print "hello again", arg, "!"

    def help_hello(self):        # 定義hello命令的幫助輸出
        print "syntax: hello [message]",
        print "-- prints a hello message"

    def do_quit(self, arg):     # 定義quit命令所執行的操作
        sys.exit(1)

    def help_quit(self):        # 定義quit命令的幫助輸出
        print "syntax: quit",
        print "-- terminates the application"

    # 定義quit的快捷方式
    do_q = do_quit

# 創建CLI實例並運行
cli = CLI()
cli.cmdloop()

根據這個例子中的注釋,我們可以知道這個模塊大部分的用法。作為CDays-2的要求,我們知道怎么用就可以了。

我們重新分析pycdc-v0.4.py

沒有定義命令提示符

do_命令   —————— 是該命令執行的函數

help_命令  —————— 是該命令的幫助函數

image

cmdloop( )  的意思是能自動返回cmd輸入命令狀態。


讓我們加入自己編寫的模塊好了。

# -*- coding: utf-8 -*-
'''pycdc-v0.5.py
Lovely Python -2 PyDay 
'''
import sys, cmd
from cdctools import *
class PyCDC(cmd.Cmd):
    def __init__(self):
        cmd.Cmd.__init__(self)                # initialize the base class
        self.CDROM = 'D:\\CDROM'
        self.CDDIR = 'D:\\'
        self.prompt="(PyCDC)>"
        self.intro = '''PyCDC 0.5 使用說明:
    dir 目錄名     # 指定保存和搜索目錄,默認是 "cdc"
    walk 文件名    # 指定光盤信息文件名,使用 "*.cdc"
    find 關鍵詞    # 遍歷搜索目錄中所有.cdc文件,輸出含有關鍵詞的行
    help          # 查詢
    EOF           # 退出系統,也可以使用Crtl+D(Unix)|Ctrl+Z(Dos/Windows)
        '''

    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("輸入cdc文件名:: ")
        print "掃描光盤內容保存到:'%s'" % filename
        cdWalker(self.CDROM,self.CDDIR+filename)

    def help_dir(self):
        print "指定保存/搜索目錄"
    def do_dir(self, pathname):
        if pathname == "": pathname = raw_input("輸入指定保存/搜索目錄: ")
        self.CDDIR = pathname
        print "指定保存/搜索目錄:'%s' ;默認是:'%s'" % (pathname,self.CDDIR)

    def help_find(self):
        print "搜索關鍵詞"
    def do_find(self, keyword):
        if keyword == "": keyword = raw_input("輸入搜索關鍵字: ")
        print "搜索關鍵詞:'%s'" % keyword
        cdcGrep(self.CDDIR,keyword)

if __name__ == '__main__':      # this way the module can be
    cdc = PyCDC()            # imported by other programs as well
    cdc.cmdloop()

image

到此為止,我們已經基本完成了CDC的除了查找外的所有功能了。

現在讓我們解決查找問題。

根據當時我們保存的內容,我們將每個文件保存為一行,所以我們可以一行一行的尋找。

def cdcGrep(cdcpath,keyword):
    filelist = os.listdir(cdcpath)          # 搜索目錄中的文件
    for cdc in filelist:                    # 循環文件列表
        cdcfile = open(cdcpath+cdc)         # 拼合文件路徑,並打開文件
        for line in cdcfile.readlines():    # 讀取文件每一行,並循環
            if keyword in line:             # 判定是否有關鍵詞在行中
                print line                  # 打印輸出

把這段程序放在cdctools.py 中就可以了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM