51qsx 发表于 2017-5-9 07:09:35

搜索输入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()

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

  示提输入.

  输入法合字符有示提.

  输入法合字符, 好吧,程序里没有,我认承我经已看晕了.
  让我们看一下书,书上说是用的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 ",
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 中以可就了。
  文章结束给大家分享下程序员的一些笑话语录: 人脑与电脑的相同点和不同点,人脑会记忆数字,电脑也会记忆数字;人脑会记忆程序,电脑也会记忆程序,但是人脑具有感知能力,这种能力电脑无法模仿,人的记忆会影响到人做任何事情,但是电脑只有程序软件。比尔还表示,人脑与电脑之间最重要的一个差别就是潜意识。对于人脑存储记忆的特别之处,比尔表示,人脑并不大,但是人脑重要的功能是联络,人脑会把同样的记忆存储在不同的地方,因此记忆读取的速度就不相同,而这种速度取决于使用的频率和知识的重要性。人脑的记忆存储能力会随着年龄增长而退化,同时记忆的质量也会随着年龄退化。经典语录网
页: [1]
查看完整版本: 搜索输入CDays–2 完成核心功能 CMD模块 Python基础教程 cmd cli 搜索输入