在指定目录下的文件中查找关键字
来源:互联网 发布:莹恋乒乓球总会淘宝店 编辑:程序博客网 时间:2024/05/17 23:34
############################################################# File: py_search.py - 查找文件里的特定字符(串)# - 遍历目录和搜索文件中的关键字# Date: 2014/08/19# By : LinJK############################################################import os import sys import cmd print( sys.argv ) #当前.py文件所在路径 class Tool(cmd.Cmd): def __init__(self): bDir = "false" cmd.Cmd.__init__(self) #Initialize the base class print ( "Please set you search directory first : ") print ( "Usage: <pathname: d:> <dirname: //dir1//dir2> ") while bDir == "false": self.pathname = input("1>Input pathname: ") self.dirname = input("2>Input dirname : ") choice = input("Ensure? y/n ") if choice == "y": bDir = "true" print( "@@Set pathname as: '%s'" %self.pathname ) print( "@@Set dirname as: '%s'" %self.dirname ) self.prompt = "(find)$" self.intro = '''--FindKeywordInFileVv0.1 usage: set # set dir and path parameters export # export result file find (keyword) # set finding keyword ? # show commands that you can enter q # exit current program, or use Ctrl+D(UNIX)|Ctrl+C(Dos/Windows) ''' elif choice == "n": continue else: print ("Please input y or n, enter again!") continue def help_q(self): print ("<HELP>Quits the program") def do_q(self, line): print ("Bye !") sys.exit() #---------------------------------------------------------------------- def help_set(self): print ("<HELP>Set parameter program used" ) def do_set(self, choice): print ( "Current para is: ") print ( "-->pathname: '%s'" %self.pathname ) print ( "-->dirname : '%s'" %self.dirname ) choice = input("Do you wanna change? y/n ") if choice == "y": print ( "Please enter you search directory : ") print ( "Usage: <pathname: d:> <dirname: //dir1//dir2> ") self.pathname = input("input pathname: ") self.dirname = input("input dirname : ") print( "@@Set pathname as: '%s'" %self.pathname ) print( "@@Set dirname as: '%s'" %self.dirname ) #---------------------------------------------------------------------- def help_find(self): #Input<help find> print("<HELP>Set finding keyword,Input: find (KeyowrdToFind)") def do_find(self, keyword): if keyword == "": #When just input<find> keyword = input("input finding keyword: ") print ("@@The keyword to find is: '%s'" %keyword) GrepFromFile(self.pathname + self.dirname, keyword) print ("<<<<<Find over !>>>>>" ) #---------------------------------------------------------------------- def help_export(self): print ("<HELP>Export result file" ) def do_export(self, para): print ("Pathname: '%s'" %sys.path[0]) #export to the root-dir where .py file exist WriteDirList(sys.path[0] + "//OS", "output.txt") #export appointed dir's info to txt file #---------------------------------------------------------------------- def help_lcd(selef): print ("<HELP>List cur path's or appointed path's info.") def do_lcd(self, para): listCurDir = os.listdir() for lstdir in listCurDir: print (lstdir) #============================================================================== # write file lists in according path def WriteDirList(path, file): print ("txt file exporting, please wait...") export = "" for root, dirs, files in os.walk(path): export += "\n%s %s %s" %(root, dirs, files) open(file, "w").write(export) print ("export over !" ) # grep keyword from txt type file only def GrepFromFile(path, keyword): listCppFiles = [] listTxtFiles = [] try: filelist = os.listdir(path) #print (filelist ) #打印当前目录下存在的所有文件,包括隐藏文件及目录 except FileNotFoundError as err: print (err) print ("Please enter \"set\" to set the correct directory!") return #对要搜索的文件进行分类 for file in filelist: if ".cpp" in file: listCppFiles.append(file) if ".txt" in file: listTxtFiles.append(file) #print (listCppFiles) #print (listTxtFiles) #分文件类型进行查找 for cppfile in listCppFiles: curfile = open(path + "//" + cppfile) # print (curfile) #curfile是文件的完整路径等信息 scanfiles(curfile, cppfile, keyword) print ("--------------------------------------------------") for txtfile in listTxtFiles: curfile = open(path + "//" + txtfile) scanfiles(curfile, txtfile, keyword) #查找文件里的关键字def scanfiles(curfile, file, keyword): bFind = "" txtfilevalidCnt = 0 print ("-->Scanning <%s>..." %(file) ) for line in curfile.readlines(): if keyword in line: txtfilevalidCnt += 1 print (" " + str(txtfilevalidCnt) + ". " + line) bFind = "true" if bFind != "true": print( " Find nothing in <%s>" %(file) ) ###################################################################### # Start ######################################################################if __name__ == '__main__': cdc = Tool() cdc.cmdloop() print( "End" )
0 0
- 在指定目录下的文件中查找关键字
- shell查找指定目录下的关键字文件
- 利用BlockingQueue,统计目录下的文件中匹配指定的关键字在什么位置
- vc__在指定目录下查找文件
- Python:在指定目录下查找满足条件的文件
- 在指定目录下,模糊查找包含标识的文件
- Python脚本实现查找指定目录下包含指定关键字的文件
- window查找指定字符串在当前目录下的所有文件中是否存在
- java实现在指定目录下的特定文件中查找特定字符串集
- LINUX 在 指定目录下,包括子目录,查找文件中内容匹配的字符串
- Objective C在目录中查找指定的文件
- java在指定路径下查找所有文件及其子文件名中,是否包含指定查询的关键字
- vc查找指定目录下的文件:
- 查找指定目录下的文件
- C++ Builder 在指定目录下查找指定文件
- 用python查找在指定目录下特定文件夹下的指定文件
- Python实现查找指定目录下包括某关键字的所有文件脚本
- 在目录中查找指定的字符串
- php面向对象之-抽象类-131
- [置顶] 回到梦想开始的地方——致我的ACM
- struts-config.xml文件中的form-bean和action-bean关系
- java Socket API 详解 .没有实例只能自己取理解了
- 2.2.2 使用不可变数据结构
- 在指定目录下的文件中查找关键字
- 关于协方差矩阵的概念及意义
- 使用ADF-BC开发业务服务
- hdu 4970
- 巧数之471
- edward_mj退役经验帖(浙大2届Final队员陈伟杰的退役贴)
- STDIN_FILENO的作用及与stdin 的区别
- STL vector中的operator[]方法(31)
- 初学maven(5)-使用assembly plugin实现自定义打包