I am trying to create a python script that will iterate through a folder structure, find folders named 'bravo', and modify the xml files contained within them.
In the xml files, I want to modify the 'location' attribute of a tag, called 'file'. Such as:
<file location="e:\one\two"/>
I just need to change the drive letter of the file path from ‘e’ to ‘f’. So that it will read:
<file location="f:\one\two"/>
However...
The name of these xml files are unique, so I cannot search for the exact xml file name. Instead I am searching by the xml file type.
Also, there are other xml files in my folder structure, without the ‘file’ tag reference, that I wish to ignore.
The only constant is that the xml files I want to modify are all stored in folders named, ‘bravo’.
I also wish to create a log file that lists all the xml files and their filepaths which have successfully been updated (and preferably the ones that failed).
Using answers to similar questions on this site, I have cobbled together the following script.
In its current state, the script trys to modify every xml files it finds. I have not been able to successfully add code that only searches folders called, ‘bravo'.
When the script modifies an xml file, not in a 'bravo' folder, it errors because these files do not contain a 'file' tag.
Please could someone help me to correct my script (or create a new one).
Here is an example of the folder structure...
And my script so far...
from xml.dom import minidom
   import os
   # enter the directory where to start search for xml files...
   for root, dirs, files in os.walk("c:/temp"):
       for file in files:
           #search for xml files...
           if file.endswith(".xml"):
               xml_file = file
               xmldoc = minidom.parse(os.path.join(root, xml_file))
               # in the xml file look for tag called "file"...
               file_location = xmldoc.getElementsByTagName("file")
               # i don't understand the next line of code, but it's needed
               file_location = file_location[0]
               # 'location_string' is a variable for the 'location' path of the file tag in the xml document
               location_string = (file_location.attributes["location"].value)
               # the new drive letter is added to the location_string to create 'new_location'
               new_location = "f" + location_string[1:]
               # replace the 'location' value of the file tag with the new location...
               file_location.attributes["location"].value = new_location
               # write the change to the original file
               with open((os.path.join(root, xml_file)),'w') as f:
               f.write(xmldoc.toxml())
               print "%s has been updated!" % (os.path.join(root, xml_file))
               # add updated file name to log...
               log_file = open("filepath_update_log.txt", "a")
               log_file.write("%s\n" % (os.path.join(root, xml_file)))
               log_file.close
 
     
     
    