1

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 
'''
8
  • Specify what you want your output to look like. Commented Dec 25, 2019 at 16:43
  • One more thing I noticed is if a .xml contains 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 a break statement right after the print(fullpath) statement. Commented Dec 25, 2019 at 16:53
  • Thanks, i want the output to give me program list like program 1 , program 2, program3 Commented Dec 25, 2019 at 17:33
  • 1
    @Ch3steR: On second reading, I realize that your example started from a bad string literal copied from the question—that doesn’t come up with file names obtained during execution. Commented Dec 25, 2019 at 18:55
  • 1
    Please accept an answer if you think it solves your problem. It will help the community at large to recognize the correct solution. This can be done by clicking the green check mark next to the answer. See this image for reference. Cheers. Commented Dec 26, 2019 at 6:01

1 Answer 1

1

From inputs given by Davis Herring in the comments. I think this should work for you.

os.path.split() method in Python is used to Split the pathname into a pair head and tail. Here, the tail is the last pathname component and the head is everything leading up to that. For example, consider this path '/home/user/Desktop/file.txt'

HEAD: '/home/user/Desktop/file.txt' and TAIL: 'file.txt'

os.path.split(fullpath) will give output like this (C:\\test1\\tool\\program3\\SETUP3\\reports\\XMLs, 'test1.xml')

NOTE: If name of the path ends with a slash \, tail will be empty and if there is no slash in path name, head will be empty.

os.path.split() return a tuple where the head is present in the index 0 and the tail in the index 1. Now simply parse the string in index 0 to get your desired folder.

head_tail=os.split.path(fullpath)  #head_tail=('C:\\test1\\tool\\program3\\SETUP3\\reports\\XMLs', 'test1.xml')

folder=head_tail[0].split('\\') #head_tail[0]='C:\\test1\\tool\\program3\\SETUP3\\reports\\XMLs'

#folder=['C:', 'test1', 'tool', 'Program1', 'SETUP1', 'reports', 'XMLs', 'test1.xml']

print(folder[3],end=",")

Now the output would print program1.Make this as a function. Pass all the paths to the functions and your output would look like what you wanted i.e. Program1, program 2, program3,

Sign up to request clarification or add additional context in comments.

1 Comment

Please accept an answer if you think it solves your problem. It will help the community at large to recognize the correct solution. This can be done by clicking the green check mark next to the answer. See this image for reference. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.