Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!

Jim ken

Ranch Hand
+ Follow
since Nov 29, 2018
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jim ken

Ok,hopefully with in next 24 hour some expert will resolve this issue on this forum.

Thanks
1 year ago
experts, could you please advise now.
1 year ago
tried this but it's showig incorrect line no.
#importing required packages

import glob
from collections import Counter
import re
import xlwt
from xlwt import Workbook
import xlsxwriter
import xlrd
import errno
import time
from datetime import datetime
import datetime
import os
import os.path
import warnings
from xlutils.copy import copy
import openpyxl

# opening excel file

from xlrd import open_workbook

warnings.filterwarnings("ignore")
timestr = time.strftime("%Y%m%d-%H%M%S")

# path where all the folders and sub folders need to be searched.

yourpath = "D:\\mainfolder\\subfolders"


# location where excel containing all the keywords which are to be searched in above path.
loc = ("D:\\sample.xlsx")

cnt = Counter()
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
rows = sheet.nrows

excel_word=[]
# loop to pick up all the keywords from the column of excel one after another.

for i in range(1,rows):
   excel_word.append(sheet.cell_value(i,1))

# report to be generated in this location.
report_txt="D:\\mainfolder\\report"+timestr+".txt"

# report is opened in write mode.

FO = open(report_txt, 'w')

# structure layout of text file where records will be written.

str3="|"+"Pattern"+" "*(20-len("Pattern"))+"|"+"Vuernabilitiy  in file"+" "*(200-len("Vuernabilitiy  in file"))+"|"+"Line No"+" "*(10-len("Line No"))+"|" +"\n"
FO.write(str3)
FO.write("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"+"\n")

# main logic

for root, dirs, files in os.walk(yourpath, topdown=False):
   
   line=0
   for name in files:
       
       path=os.path.join(root, name)
       files = glob.glob(path)
       
       for name in files:
               
               
               try:
                   with open(name,encoding="utf8",errors='ignore') as f:
                       
                       text_string1 = f.read()
                       for i in range(0,len(excel_word)):
                           str2=''
                           for num, line in enumerate(name, 1):
                               if excel_word[i] in text_string1:
                                   cnt[excel_word[i]] += 1
                           
                                   str2="|"+excel_word[i]+" "*(20-len(excel_word[i]))+"|"+os.path.join(root, name)+" "*(200-len(os.path.join(root, name)))+"|"+str(num)+" "*(10-len(str(num)))+"|" +"\n"
                               
                           else:
                               cnt[excel_word[i]]+=0
                           FO.write(str2)
                           
                           
                                         
               except IOError as exc:
                   if exc.errno != errno.EISDIR:
                       raise

FO.close()
1 year ago
if no help can be offered then why this forum have been opened and code is self explanatory any python expert while reading it will be able to tell where is the mistake if this forum is not for help for users then what the benifit of this forum,this forum itself be closed.
1 year ago
Any updates please?
1 year ago
code is supposed to give result in a set format [keyword1,found in file name(at this lines),found in file name(at this line),(total found count of keyword1)] so for all the keywords which are listed in excel file column 'A' it should give these results that too in a graph format in html file (containing count etc. for keywords) but it's failing whle execution time itself giving 'invalid code identifier'.

Thanks
1 year ago
ya, but any quick assistance would be much appreciated on this.

Thanks
1 year ago
any updates by python experts please?
1 year ago
python experts could you please help on the same?
1 year ago
As stated earlier in this post script should be able to search all type of files like .txt,.yml,.xml,.html,.sh ...etc. in all the folders and subfolders for that given path in system's D drive and then finally be able to append the count column which has total counts for all the corresponding found keywords from this excel (Here let's say Column 'A' in this excel has all these keywords in it) so against column A it should list count and in next column it should list files where it found these key words and in next column it should list file names with the line nos for the found keyword.

Thanks
1 year ago
also tried below script as well but it's also not working:-

import os
import glob

def search_words(keyword, target_dir):
   files = glob.glob(target_dir + '/**', recursive = True)
python_files = []
results = []
line_no = []# Isolate target files from folders and everything
else
   for f in files:
   if f.endswith('.py'):
   python_files.append(f)

for pyf in python_files:
   with open(pyf, 'rb') as f:
   lines = f.readlines()
for i, line in enumerate(lines):
   line = str(line)
if line.find(keyword) > -1:
   line_no.append(i)

results.append({
   'keyword': keyword,
   'lines': line_no,
   'target_file': pyf,
   'total_found': len(line_no)
})
return results
1 year ago