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

any updates by python experts please?
57 minutes ago
python experts could you please help on the same?
3 hours 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
3 hours 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
3 hours ago
Script needs to read excel file which has let's say column A having these set of keywords let' s say 100 such keywords in this column and
after reading these keywords from this column 'A' python script should search them to a particular path in D drive for all folders and subfolders
containing miscellaneous file types(.xml,.txt,.html,.yml,.sh...etc.)and once match is found lets say for first keyword it finds
a match in particular file at specific line number in this file and again it finds the same keyword in another file at some other line number
so here totally it was found 2 times and at different line nos in different files but it may also be the scenario that same keyword in same file
is found more than once on same line no itself or at different line no as well in the same file. So we need finally this statics for this keyword :- 1)
File names where it was found 2) At what lines ( it may be repeated more than one line nos as it may depend on the frequency of that particular keyword
how many times it's occuring in a single file itself ) in these files it was found. 3)  
Total count of that particular keyword where all it was found while searching all the file in the
given disk drive for all the files,folders,subfolders in it.
also after reading this excel file it should write the found these details :-1)Keyword matching file names 2) All the line nos where all
it was found with their file names as well 3) Total count for each keyword which were found different no. of times while searching in this
D drive( which has different types of files in it) for all the files,folders,subfolders in it.

Tried below code but it's not producing desired output:-

yourpath = 'D:\\Tool devlopment\\tool\\Folder
import glob
from collections
import Counter
import re
import errno
import os
import time
from datetime
import datetime
import datetime
import warnings
warnings.filterwarnings("ignore")
timestr = time.strftime("%Y%m%d-%H%M%S")

file2 = open("D:\\Tool devlopment\\tool\\Module\\excel.txt", 'r')
text_string1 = file2.read().lower()
word2 = text_string1.split()
sast_txt = "D:\\Tool devlopment\\tool\\Module\\SAST_ScanReport" + timestr + ".txt"
FO = open(sast_txt, 'w')
cnt = Counter()
for root, dirs, files in os.walk(yourpath, topdown = False):
   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().lower()
word1 = re.split("[^a-zA-Z]*", text_string1)
for i in range(0, len(word2)):
   if (word2[i] in set(word1)):
       cnt[word2[i]] += 1
str2 = "Vuernabilitiy found at line: " + str(i + 1) + ' in file ' + os.path.join(root, name) + "\n"
FO.write(str2)
str2 = ''
3 hours ago
Any updates by experts on the same please?
6 hours ago
also tried this one but this also did not work as expected :-
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
21 hours ago
tried below code but it's not writing in desired format for excel:-
yourpath = 'D:\\Tool devlopment\\tool\\Folder
import glob
from collections
import Counter
import re
import errno
import os
import time
from datetime
import datetime
import datetime
import warnings
warnings.filterwarnings("ignore")
timestr = time.strftime("%Y%m%d-%H%M%S")

file2 = open("D:\\Tool devlopment\\tool\\Module\\excel.txt", 'r')
text_string1 = file2.read().lower()
word2 = text_string1.split()
sast_txt = "D:\\Tool devlopment\\tool\\Module\\SAST_ScanReport" + timestr + ".txt"
FO = open(sast_txt, 'w')
cnt = Counter()
for root, dirs, files in os.walk(yourpath, topdown = False):
   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().lower()
word1 = re.split("[^a-zA-Z]*", text_string1)
for i in range(0, len(word2)):
   if (word2[i] in set(word1)):
       cnt[word2[i]] += 1
str2 = "Vuernabilitiy found at line: " + str(i + 1) + ' in file ' + os.path.join(root, name) + "\n"
FO.write(str2)
str2 = ''


except IOError as exc:
   if exc.errno != errno.EISDIR:
   raise
for m in cnt:
   str3 = ''
str3 = str(m) + ' ' + str(cnt[m]) + ' ' + 'time' + '\n'
FO.write(str3)
FR = open(sast_txt, 'r')
sast_file = "D:\\Tool devlopment\\tool\\Module\\SAST_ScanReport" + timestr + ".html"
with open(sast_file, "w") as e:
   for lines in FR.readlines():
   e.write("<pre>" + lines + "</pre> <br>\n")

FO.close()
21 hours ago
How to show a found keyword in a particular path in a drive (which should search all files ,folders,subfolders in it)
and it's found line numbers( for that particular set of keywords which are listed in a excel file) in a set form
like [{keyword1, found at this file name 1 (line no1, line no2,...line no. n), found at this file name2 (in line1,line 2...line no. n),
found at this file name3 (in line no1,line2...line no. n),found at this file name n ( in line no1,line no2,line no. n),
(it's Total found count), similarly for all the keywords it should write desired results in a excel file from which it's starting to read
all these keywords which are listed in a particular column.
and then need to write it this data in a excel file with the column headings like keywords total found count, found in these files
(with their found line nos as well in these files along with the file name and line nos where it was exactly found)
21 hours ago
Ok, then can some admin please delete my all posts regarding this query from this forum.
1 week ago
Any updates by experts please?
1 week ago