# -*- 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 -*-
'''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 中就可以了。