I am new to python programming. I am trying to make tool to extract program name(folder name ) after doing recursive search of a string in files of particular extension
i got data like this after printing the path of files having a string which i am searching in xml files. Now i want to extract the program name for example Program1, program 2 , program
import os
search_path = input("Enter directory path to search : ")
for folder, dirs, files in os.walk(search_path):
for file in files:
if file.endswith('.xml'):
fullpath = os.path.join(folder, file)
with open(fullpath, 'r') as f:
for line in f:
if "test" in line:
print(fullpath)
output is like
C:\test1\tool\Program1\SETUP1\reports\XMLs\test1.xml
C:\test1\tool\Program1\SETUP1\reports\XMLs\test1.xml
C:\test1\tool\Program1\SETUP1\reports\XMLs\test1.xml
C:\test1\tool\program2\SETUP2\reports\XMLs\test2.xml
C:\test1\tool\program2\SETUP2\reports\XMLs\test2.xml
C:\test1\tool\program3\SETUP3\reports\XMLs\test2.xml
C:\test1\tool\program3\SETUP3\reports\XMLs\test3.xml
C:\test1\tool\program3\SETUP3\reports\XMLs\test3.xml
data
i want to extract the program name and setup from above data
'''
.xmlcontains more than one"test"word in it the path to that file is printed the number times"test"word is in it. You eliminate that by adding abreakstatement right after theprint(fullpath)statement.