0

i was trying to do this for a while and i cant find a way to do it. i need to search a string in my xml files that says 'drone' and than move all the files that contains the string to another directory

this is what im working with:

    import os
    import shutil

print("Contents of source and destination before moving:")   
source = r"D:\\TomProject\\Images"   
destination = r"D:\\TomProject\\txt"   
for root, dir, files in os.walk(source):   
    print(root)   
    print(dir)   
    print(files)   
 print(os.listdir(destination))   
for root, dir, files in os.walk(source):   
    for file in files:   
        if ".xml" in file:  # checking if the file is a xml file by looking for .xml  
  extension in the name of the file          
           shutil.move(os.path.join(root, file), destination)   
    
print("Contents of destination after moving:")   
print(os.listdir(destination))   
  • maybe this question will help: https://stackoverflow.com/questions/10477294/how-do-i-search-for-a-pattern-within-a-text-file-using-python-combining-regex – krock Oct 24 '22 at 09:00
  • From what I understand, the question has 3 parts: a) Loop through all the `xml` files in a directory, b) Check for a certain string, and c) Move the file. What step exactly are you struggling with? – Standard_101 Oct 24 '22 at 09:06
  • right now im struggling with part b, how i check for a certain string in the file – tom meskin Oct 24 '22 at 09:41
  • Does [this](https://stackoverflow.com/a/4944929/15740324) help with that? For part (c) maybe [this](https://stackoverflow.com/a/8858026/15740324) would be helpful. – Standard_101 Oct 24 '22 at 09:46
  • For search strings on XML files, I recommend you ElementTree. With ElementTree you can search for tags, attributes, or text. – Loxley Oct 24 '22 at 09:59

0 Answers0