您的位置:首页 > 编程语言 > Python开发

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

2013-02-14 19:36 856 查看
再过两个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()


根据书上所述,我们不知道上面的一些语法是什么,的确,我们确实不知道。

让我们运行一下.





提示输入.





输入非法字符有提示.





输入合法字符, 好吧,程序里没有,我承认我已经看晕了.

让我们看一下书,书上说是用的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_命令  —————— 是该命令的帮助函数





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






到此为止,我们已经基本完成了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 中就可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: