在指定目录下的文件中查找关键字

本文介绍了一个Python脚本,用于在指定目录及其子目录中查找特定字符或字符串。脚本通过命令行交互设置搜索目录和参数,支持对`.cpp`和`.txt`文件进行关键字搜索,并将结果输出到文本文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

############################################################
# 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" ) 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值