Pythonはパスファイルから特定のコンテンツに一致するファイルを見つけます


需要説明:特定のパスの下のファイルから、特定の文字を含むファイルを見つけ、指定したディレクトリの下に入れ、一致するファイルのexcelテーブルを出力し、ファイルエラーのテーブルコードを開くと、次のように実現されます.
import os 
import shutil
from openpyxl import Workbook
import time
wb = Workbook()
ws1 = wb.create_sheet('       ')
def print_list(name,l):#      
    print(name)
    for i in l:
        print(i)
	
def find_specific_file(file_path,s): #          
    if not os.path.exists(file_path):
        print('     ,     ')#      
        return None
    if not isinstance(s,str):
        print('            ,     ')#      
        return None
    file_error_list=[]
    file_find_list=[]
    file_all_count=0
    file_find_count=0
    file_error_count=0
    ws1.cell(row=1,column = 1, value='      ')
    ws1.cell(row=1,column = 2, value='        ')
    i=1
    j=1
    for root,dirs,files in os.walk(file_path):
        for file in files:
            file_all_count+=1
            try:
                try:
                    with open(file_path+'\\'+file,'r',encoding='ANSI') as fp:
                        content = fp.read()			
                        if s in content:
                            #print(file) #  file   
                            file_find_list.append(file)#          
                            shutil.copyfile(file_path+'\\'+file,'e:\\file_find'+'\\'+file)#             
                            file_find_count+=1 #      
                            i+=1
                            ws1.cell(row=i,column = 1, value=file)#          excel   
                except:
                    with open(file_path+'\\'+file,'r',encoding='utf-8') as fp:
                        content = fp.read()			
                        if s in content:
                            #print(file) #  file   
                            file_find_list.append(file)#          
                            shutil.copyfile(file_path+'\\'+file,'e:\\file_find'+'\\'+file)#             
                            file_find_count+=1 #      
                            i+=1
                            ws1.cell(row=i,column = 1, value=file)#          excel     
            except Exception as error:
                #print(error)
                #print('         :%s' %file)
                file_error_list.append(file)#          
                file_error_count+=1 #      
                j+=1
                ws1.cell(row=j,column = 2, value=file) #          excel    

    print('    %s ,       %s ,       %s' %(file_all_count,file_find_count,file_error_count))
    print_list('      :',file_find_list)
    #print_list('        :',file_error_list)
    wb.save('e:\\find_file.xlsx')    
    return None

time1=time.time()
find_specific_file('e:\\config','lns')#    
time2=time.time()
print('  :',time2-time1) #    
find_specific_file('e:\\config',['aaaaaaaa'])#      
find_specific_file('e:\\config\\2jfa','lns')#