这书看得挺顺的。
按着思路学习软件开发的主流思想。。
cdctools.py
# _*_ coding: utf-8 _*_ import os,sys def cdWalker(CDROM, cdcfile): export = "" for root, dirs, files in os.walk(CDROM): export += formatCDinfo(root,dirs,files) open(cdcfile,'w').write(export) def cdcGrep(cdcpath, keyword): filelist = os.listdir(cdcpath) for cdc in filelist: if ".cdc" in cdc: cdcfile = open(cdcpath + cdc) for line in cdcfile.readlines(): if keyword in line: print(line) def formatCDinfo(root,dirs,files): export = "\n" + root + "\n" for d in dirs: export += "-d " + root + d + "\n" for f in files: export += "-f %s %s \n" %(root,f) export += "=" * 70 return export if __name__ == '__main__': CDROM = 'E:\\TDDOWNLOAD\\' cdWalker(CDROM,'cdc.cdc') ''' if sys.argv[1] == '-e': cdWalker(CDROM, sys.argv[2]) print("记录文件夹 %s 信息内容 到 %s" % (CDROM, sys.argv[2])) else: print('PYCDC使用方式: python pycdc.py -e mycdc1-1.cdc 将光盘内容记录到mycdc1-1.cdc ') '''
pycdc-v-0.5.py
# _*_ coding: utf-8_*_ import sys, cmd from cdctools import * class PyCDC(cmd.Cmd): def __init__(self): cmd.Cmd.__init__(self) self.CDROM = 'E:\\TDDOWNLOAD\\' self.CDDIR = 'cdc\\' self.prompt = "(PyCDC)>" self.intro = '''PyCDC0.5使用说明: dir 目录名 #指定保存和搜索目录,默认是"cdc" walk 文件名 #指定光盘信息文件名,使用"*.cdc" find 关键词 #使用在保存和搜索目录中遍历所有的.cdc文件,输出含有关键词的行 ? #查询 EOF #退出系统,也可以使用Ctrl + 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 = input("请输入CDC文件名: ") print("扫描光盘内容到: '%s'" % filename) cdWalker(self.CDROM,self.CDDIR + filename) def help_dir(self): print("请拽定保存/搜索目录") def do_dir(self, pathname): if pathname == "":pathname = input("请输入指定保存/搜索目录: ") self.CDDIR = pathname print("指定保存/搜索目录:'%s' ; 默认是: '%s'" % (pathname, self.CDDIR)) def help_find(self): print("搜索关键词") def do_find(self, keyword): if keyword == "":keyword = input("请输入搜索关键字: ") print("搜索关键词:%s" % keyword) cdcGrep(os.getcwd() + "\\" + self.CDDIR, keyword) if __name__ == '__main__': cdc = PyCDC() cdc.cmdloop()
输出:
时间: 2024-11-08 21:21:32